即使域不匹配,也可以访问iframe的父级 [英] Access parent from iframe even though domains don't match?

查看:277
本文介绍了即使域不匹配,也可以访问iframe的父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在尝试访问父项的iframe中运行的JavaScript脚本,但是我收到以下错误:

I have a javascript script that is being run from within an iframe that is trying to access the parent but I'm getting the following error:

Unsafe JavaScript attempt to access frame with URL mysite.com from frame with URL myothersite.com?. Domains, protocols and ports must match.

iframe html是在不同的域,但我不认为这将是重要的。这是生成JS错误的代码:

The iframe html is on a different domain but I didn't think that would matter. This is the code that is generating the JS error:

var parent_site = parent.document;

有没有办法?

推荐答案

如果父域是iframe域的尾部(即iframe是child.parent.com而父对象parent.com),您可以使用 document.domain =parent.com设置iframe文档的域,并避免出现此问题。

If the parent domain is a trailing part of the iframe domain (i.e., iframe is child.parent.com and parent is parent.com) you can set the domain of the iframe document with document.domain = "parent.com" and avoid the problem.

如果父级和iframe的域名无关,那么无法解决问题。

If the domains of the parent and the iframe are unrelated there is no way to work around it.

这篇关于即使域不匹配,也可以访问iframe的父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆