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>
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.
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With