Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create iframe with javascript appending to a div

I want to load/append the iframe into a div with regular javascript. I can do this with JQuery without a problem, but I dont want to include the js file. I keep getting the error 'document.getElementById("ad54")' (or whatever id I assign the div). I have the following code:

var link = "http://www.google.com"
var iframe = document.createElement('iframe');
iframe.frameBorder=0;
iframe.width="300px";
iframe.height="250px";
iframe.id="randomid";
iframe.setAttribute("src", link);
document.getElementById("ad54").appendChild(iframe);

<div id="ad54"></div>
like image 881
Patriotec Avatar asked Apr 24 '12 16:04

Patriotec


People also ask

How do I add content to an iframe?

Answer: Use the jQuery contents() method You can use the jQuery contents() method in combination with the find() , val() and html() methods to insert text or HTML inside an iframe body.

Does HTML5 support iframe?

HTML 5 <iframe> Tag. The HTML <iframe> tag is used to specify an inline frame, or, as the HTML5 specification refers to it, a nested browsing context. An inline frame allows you to embed another document within the current HTML document.


1 Answers

You should write this inside window.onload like

window.onload = function(){
   var link = "http://www.quirksmode.org/iframetest2.html"
var iframe = document.createElement('iframe');
iframe.frameBorder=0;
iframe.width="300px";
iframe.height="250px";
iframe.id="randomid";
iframe.setAttribute("src", link);
document.getElementById("ad54").appendChild(iframe);

}
like image 160
Sethunath K M Avatar answered Sep 19 '22 15:09

Sethunath K M