[英]How to insert a javascript file into an iframe then call a function in the inserted javascript?
[英]How to javascript call iframe loaded file function
我在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>
而這個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>
我想在index.html中顯示警報。 但這沒有用。我該怎么辦? 請幫我
謝謝!
這被認為更可靠,並且可能會起作用:
<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>
還要確保alt和index.html在使用相同協議(HTTP或HTTPS)的同一域中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.