调用javascript函数TypeError时出错:i.contentWindow.hiform不是函数

error when call javascript function TypeError: i.contentWindow.hiform is not a function

本文关键字:函数 contentWindow hiform 出错 javascript TypeError 调用      更新时间:2023-09-26

我有两页主页面的第一页,包含两个框架和三个按钮在第一帧中将src设置为第二页
加载帧后,我将第一帧内容复制到第二帧中,并从主页调用第二帧内的函数,但错误TypeError:i.contentWindow.hiform不是函数

主页代码

 <script type="text/javascript">
     function CallFunction(frameName)
        {
                var i = document.getElementById(frameName);
                i.contentWindow.hiform();
     }
     function Copy() {
         try {
             var i = document.getElementById('frame1');
             var i2 = document.getElementById('frame2');
             i2.contentWindow.document.body.parentElement.innerHTML = i.contentWindow.document.body.parentElement.innerHTML;
         }
         catch (er) {
             alert(er);
         }
     }
    </script>
<iframe id="frame1" src="Second.aspx" height="100px" width="100%"></iframe>
    <iframe id="frame2"  height="100px" width="100%"></iframe>
     <input type="button" id="runFunc" onclick="CallFunction('frame1');" value="Call Function Frame1" /> this work
    <input type="button" id="Copy1" onclick="Copy();"  value="Copy"/>
     <input type="button" id="runFunc2" onclick="CallFunction('frame2');" value="Call Function Frame1" />
</div>

第二页

 <script type="text/javascript">
    function hiform() {
        alert('wow');
    }
</script>
<div>
HIIIIIIII
</div>

主要问题是您的第一页javascript在第二页中不可访问,您需要有它的引用。这就是为什么您需要分离javascript的代码,并将其移动到外部文件,然后将该文件引用到第二个页面,然后一切正常。

希望能有所帮助。