Fred the Man Fred the Man - 10 months ago 83
Javascript Question

Create an iframe with only javascript

I have been struggling on one of those programing challenge sites for a while. It has a console type thing that lets me enter javascript and press a run button. I need to create an iframe inside of that webpage with another webpage inside of it (for example, im on and I need to create the iframe with inside of it).

Have tried creating iframes as follows:

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

However it does not run when I try this, is there an alternative way?

Answer Source

Use createElement method of document

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

I just append the iframe to body. To append it to someDiv replace last line with below code