代码之家  ›  专栏  ›  技术社区  ›  kishore

如何在IFrame加载时执行javascript代码

  •  3
  • kishore  · 技术社区  · 15 年前

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

    我不能让警报工作。有人能帮帮我吗!

    3 回复  |  直到 14 年前
        1
  •  2
  •   kishore    15 年前

    我让它工作了

    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();
    
        2
  •  0
  •   David    15 年前

    .innerHTML仅用于文本更改。。。从另一个*.html文件调用脚本。或者在iframe的末尾添加onload,如下所示:

    <iframe src="test.html" onload="alert('Success')";>
    
        3
  •  0
  •   A.Badger    12 年前

    下面是一个使用jQuery的示例,我使用jQuery在加载帧时执行javascript,在重定向用户下载zip文件时使用它,在出现下载对话框后控制浏览器。

    向将保存iframe的页面添加div:

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

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

    根据需要更改url和loadFunction。