最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to execute javascript code on IFrame load - Stack Overflow

programmeradmin0浏览0评论

I am dynamically creating an iframe with javascript inside the body.

iframe = m_oYDOM.get("ifAdwords");

iframe.contentWindow.document.body.innerHTML = <script type=\"text/javascript\">function Test(){alert(\"Success\");Test();}</script>";

I am not able to get the alert to work. Can someone help me!

I am dynamically creating an iframe with javascript inside the body.

iframe = m_oYDOM.get("ifAdwords");

iframe.contentWindow.document.body.innerHTML = <script type=\"text/javascript\">function Test(){alert(\"Success\");Test();}</script>";

I am not able to get the alert to work. Can someone help me!

Share Improve this question edited Jun 18, 2012 at 19:04 Srikar Appalaraju 73.8k55 gold badges219 silver badges265 bronze badges asked Jul 20, 2010 at 20:04 kishorekishore 73710 silver badges29 bronze badges 2
  • 1 see this question: stackoverflow./questions/251420/… – bimbom22 Commented Jul 20, 2010 at 20:06
  • The above link is about calling a function from the parent page. But I want the function to be executed on iframe load – kishore Commented Jul 20, 2010 at 20:17
Add a ment  | 

3 Answers 3

Reset to default 2

I got this working

iframe = document.getElementByID("iFrameID");
var oDoc = iframe.contentWindow.document;
oDoc.open();
oDoc.write('<html><body><script type=\"text/javascript\">function Test(){alert(\"success\");}Test();<\/script><\/body><\/html>');
oDoc.close();

.innerHTML is only for text changes... Call the script from another *.html file. OR just add onload to the end of iframe like so:

<iframe src="test.html" onload="alert('Success')";>

Heres an example using jQuery that I use to execute javascript when a frame has loaded, I use it when redirecting the user to download a zip file, to control the browser after the download dialog appears.

Add a div to the page that will hold the iframe:

    <div style='height: 1px; width: 1px; border: 0;' id='iframediv'></div>

Then call the following to create the iframe with the onload action:

     var url = "http://google."
     $('#iframediv').html('<IFRAME id="downloadAlbum" onload="loadFunction()" src="'+url+'"></iframe>');

Change url and loadFunction as required.

发布评论

评论列表(0)

  1. 暂无评论