JavaScript-如何从主页执行iframe函数?
如何从main.php执行button_one()函数 Main.php:JavaScript-如何从主页执行iframe函数?,javascript,jquery,iframe,Javascript,Jquery,Iframe,如何从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> &
<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>
主页-用于操作
内页-用于按钮
$(文档).ready(函数(){
//默认情况下已加载
$('#layer').attr('src',”http://localhost/iframepage.php)show();
//稍后,单击向iframe发送命令
$('#b1')。单击(函数(){
//如何从这里执行iframepage.php按钮\ one()脚本?
});
});
iframepage.php:
<b>Welcome</b>
<script>
function button_one() {
alert("button press from main page");
}
</script>
欢迎您
功能按钮_one(){
警报(“从主页面按下按钮”);
}
试试这个:
window.frames["layer"].window.button_one()
但请注意,只有当iframe
src与您执行的脚本位于同一个域(在您的情况下是localhost
)时,这才有效。端口也应与协议相同。否则,由于安全限制,浏览器将阻止此行为。使用:
document.getElementById('layer').contentWindow.button_one();
我得到了以下信息:
Uncaught SecurityError:阻止了一个带有原点的帧”http://pbx:80“从访问具有原点的帧”http://iiiiii:8080". 协议、域和端口必须匹配代码>正如我在回答中所说,域、协议和端口应该匹配。如果没有,由于安全问题,无法从iframe调用父窗口中的函数。好的-谢谢,我现在明白了。这就是为什么不允许。如果我将:www.antyrat.com
作为主页,并且iframe页面位于:iframe.antyrat.com
,它会起作用吗?(在多媒体和voip服务器的情况下,很多带宽是相关的,因此我想从另一台服务器加载iframe,该服务器是我的,也来自同一个组织)@YumYumYum是的,但您应该添加document.domain='antyrat.com'代码>到脚本标记中的两个页面。然后,他们将进入同一个主域,并将能够相互通信。请注意,这应该没有www
,因此所有子域都可以工作。