[英]JavaScript - how can i execute a function of iframe, from main page?
How can i execute the button_one() function from main.php ? 如何从main.php执行button_one()函数?
Main.php: 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: iframepage.php:
<b>Welcome</b>
<script>
function button_one() {
alert("button press from main page");
}
</script>
Try this: 尝试这个:
window.frames["layer"].window.button_one()
But note that this will only work if iframe
src is on same domain as your executed script ( in your case localhost
). 但请注意,这仅在iframe
src与您执行的脚本位于同一域(在您的情况下为localhost
)时才有效。 Ports should be the same too as the protocol. 端口应该与协议相同。 Otherwise browser will block this behaviour due to security restrictions. 否则,由于安全限制,浏览器将阻止此行为。
采用:
document.getElementById('layer').contentWindow.button_one();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.