簡體   English   中英

JavaScript - 如何從主頁面執行iframe的功能?

[英]JavaScript - how can i execute a function of iframe, from main page?

如何從main.php執行button_one()函數?

Main.php:

<b>Main page - for the actions</b>
<div style="position: absolute; right:9%;top:30px;">      
   <input type=button id=b1 />
</div>

<b>Inner page - for the buttons</b>
<iframe id="layer" src="" ></iframe>

<script>
$(document).ready(function() {
  // by default loaded
  $('#layer').attr('src', "http://localhost/iframepage.php").show();

  // later on click send command to iframe
  $('#b1').click(function() {
      // how to execute the iframepage.php button_one() script from here?
  });
});
</script>

iframepage.php:

<b>Welcome</b>
<script>
function button_one() {
 alert("button press from main page");
}
</script>

嘗試這個:

window.frames["layer"].window.button_one()

但請注意,這僅在iframe src與您執行的腳本位於同一域(在您的情況下為localhost )時才有效。 端口應該與協議相同。 否則,由於安全限制,瀏覽器將阻止此行為。

采用:

document.getElementById('layer').contentWindow.button_one(); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM