Create an iframe with javascript only

For some time I struggled with one of those software sites. It has a console type that allows me to enter javascript and hit the start button. I need to create an iframe inside this webpage with another webpage inside it (e.g. im on thiswebsite.com/level1, and I need to create an iframe with thiswebsite.com/level2 inside it).

We tried creating an iframe as follows:

document.getElementById('someDiv').innerHTML = '<iframe src="thissite.com/level2/" height= 300 width=400>';

However, this does not work when I try to do this, is there an alternative way? Thank.

+4
source share
2 answers

Use createElementmethoddocument

var a = document.createElement('iframe');
a.src = "your path will go here"; //add your iframe path here
a.width = "1000";
a.height = "500";
document.querySelector('body').appendChild(a)

iframe body. someDiv,

document.getElementById('someDiv').appendChild(a);
+5

I , iframes.

, Console

    //define function
    function prepareiFrame() {
        var ifrm = document.createElement("iframe");
        ifrm.setAttribute("src", "https://google.com/");
        ifrm.style.width = "640px";
        ifrm.style.height = "480px";
        document.body.appendChild(ifrm);
    }

    //call it
    prepareiFrame()

Stackoverflow :

'https://www.google.co.in/?gfe_rd=cr&ei=OoWPV7agCuHt8wf3v6egDA' , 'X-Frame-Options' 'SAMEORIGIN'.

0

Source: https://habr.com/ru/post/1648521/


All Articles