开发者

Adding an onload event to body of iFrame does not work

I am trying to dynamically add an iFrame to a page and insert a 'script' tag to the 'head' tag of the iFrame. I am trying to do this through adding an onload event to the body of the iFrame so that the loading javascript i'm appending doesn't slow down the speed of the page. But it doesn't 开发者_Python百科seem to work (or actually add the onload event) in Chrome (or iPhone Safari or Android emulator).

I have successfully created the iFrame with javascript and using the code below trying to add the script tag to the iFrame head:

var iframeTag = document.getElementById('myIframe');
doc = iframeTag.contentWindow.document.open();
doc.write("<body onload=\'var d=document;var h=d.getElementsByTagName(\'head\')[0];var sc=h.appendChild(d.createElement(\'script\'));sc.language=\'javascript\';sc.src=\'iframe.js\';\'");
doc.close();


Are you sure that it's because the online event isn't being excuted? Your quote escaping looks wrong and you're not closing the body tag. Try with and se if that works.

doc.write("<body onload='alert(1234)'>");


use window.onload instead it will absolutely work !!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜