Html中iframe跨域调用js函数解决办法

页面a.html域名为www.a.com嵌入页面http://www.b.com/b.html,b.html要调用a.html中的js函数,由于两个页面不在一个域中,会提示没权限。如何解决该问题呢,请看下面示例代码。

a.html内容:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>A</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<script type="text/javascript">
function test(){
alert(123);
}
</script>
</head>
<body>
主页面 <br>
<iframe src="http://www.b.com/b.html" width="300" height="300"></iframe>
</body>
</html>

b.html内容:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>B</title>
<script type="text/javascript">
function t(){
var iframeTag = document.getElementById('frameC');
iframeTag.src = 'http://www.a.html/c.html?time='+new Date();
};
</script>
</head>
<body>
被嵌入页面,与主页面不在同一域名下。
<button οnclick="t();">Test</button>
<iframe id="frameC" style="display: none;"></iframe>
</body>
</html>

c.html内容(c.html与a.html在同一个域下):
<script type="text/javascript">
top.test();
</script>

通过代码可以很明显的看出,c.html起着关键作用,在两个域之间做为中转站。通过此方法,可以轻松解决iframe下跨域调用js函数的问题。


http://www.jiguu.com/showArticle.action?kindid=f5a9793032dda73b0132e3da96ed0016&id=f5a97930340f7d1d0134179d7e700004
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值