简体   繁体   English

如何javascript调用iframe加载文件功能

[英]How to javascript call iframe loaded file function

i have this code on index.html 我在index.html上有此代码

<body>
    <iframe id=iframetest src="alt.html">
        <p>Your browser does not support iframes.</p>
    </iframe>
    <script>
        $(window).load(function(){
             document.getElementById('iframetest').contentWindow.myFunction();
        });
    </script>
    <button onclick="myFunction();">Click Me</button>
</body>

and this 1 is in alt.html 而这个1在alt.html中

<head>
    <title>JavaScript Test</title>
    <script type="text/javascript">
        //When click on the button function will run
        function myFunction(){
        alert("Hello World");
        }
    </script>
</head>
<body>
    <h1>TEST</h1>
</body>

i want to display alert in index.html. 我想在index.html中显示警报。 but this was not worked.how can i do this? 但这没有用。我该怎么办? pleas help me 请帮我

thank you! 谢谢!

This is considered more reliable and might work: 这被认为更可靠,并且可能会起作用:

<body>
    <iframe name=iframetest src="alt.html">
        <p>Your browser does not support iframes.</p>
    </iframe>
    <script>
        $(window).load(function(){
             window.frames['iframetest'].myFunction();
        });
    </script>
    <button onclick="myFunction();">Click Me</button>
</body>

Here's a link that will explain it. 这是一个解释它的链接

also make sure alt and index.html are within the same domain using the same protocol (HTTP or HTTPS) 还要确保alt和index.html在使用相同协议(HTTP或HTTPS)的同一域中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM