I want to add an iframe
to the page. This iframe
should refer to a URL. I added the below code to page HTML, but it doesn\'t work:
docu
Here you go:
var iframe;
iframe = document.createElement('iframe');
iframe.src = 'http://example.com/file.zip';
iframe.style.display = 'none';
document.body.appendChild(iframe);
Live demo: http://jsfiddle.net/USSXF/2/
Your code doesn't work because you're passing an entire HTML string into the createElement
function ("jQuery style" :)
), which is invalid. The valid parameter for this function is a string representing the tag-name (like 'div'
, 'iframe'
, 'p'
, etc.).
Read about document.createElement here.