动态将document.domain设置为iframe [英] Dynamically set document.domain to iframe
问题描述
我有一个插入页面的iframe,称为帮助者".因此,由于具有相同的来源政策,因此我需要将iframe域设置为与父窗口域相同.但是我无法访问父窗口域.怎么解决?
I have an iframe that injects in pages, called him "helper". So due to same origin policy I need to set iframe domain the same is parent window domain. But I can't get access to parent window domain. How can it be solved?
此代码当前适用于第二级域:
This code is currently working for 2nd level domains:
pathArray = window.location.host.split('.');
var arrLength = pathArray.length;
var domainName = pathArray.slice(arrLength - 2, arrLength).join('.');
document.domain = domainName;
但是我需要以某种方式从父窗口获取它,而不是依赖于第二级域
but I need to somehow get it from parent window rather than relying on 2nd level domain
推荐答案
我不知道是否有帮助,但是我在iframe中使用了它
I do not know if it will help but i use this in iframe
try {
var domainName = window.parent.parent.iframeData.domainName;
}
//Access violation
catch (err) {
document.domain = window.location.hostname.replace('www.', '');
}
所以我检查是否已经设置了域,但我们有例外ang尝试猜测域,无论哪种情况,都无需设置域
So i check if domain already set we have exception ang try to guess domain, in either case, there is no need to set a domain
更正确地使用帖子根据需要设置域
More correctly to use post message to set domain if needed
这篇关于动态将document.domain设置为iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!