admin管理员组文章数量:1200986
I'm looking for a cross-browser way to detect whether the parent frame is the same origin as my frame, preferably without printing warnings on the JavaScript error console.
The following seems to work but I'd like to avoid printing errors to the console (at least Safari and Chrome do when accessing location.href on the parent frame. Firefox throws an exception which can be caught):
function parentIsSameOrigin() {
var result = true;
try {
result = window.parent.location.href !== undefined;
} catch (e) {
result = false;
}
return result;
}
I'm looking for a cross-browser way to detect whether the parent frame is the same origin as my frame, preferably without printing warnings on the JavaScript error console.
The following seems to work but I'd like to avoid printing errors to the console (at least Safari and Chrome do when accessing location.href on the parent frame. Firefox throws an exception which can be caught):
function parentIsSameOrigin() {
var result = true;
try {
result = window.parent.location.href !== undefined;
} catch (e) {
result = false;
}
return result;
}
Share
Improve this question
asked Apr 5, 2010 at 0:13
tlrobinsontlrobinson
2,8584 gold badges33 silver badges35 bronze badges
3 Answers
Reset to default 11I would do something like:
var sameOrigin;
try
{
sameOrigin = window.parent.location.host == window.location.host;
}
catch (e)
{
sameOrigin = false;
}
return sameOrigin;
I use this method to tell if an iframe contains local content,
but you can pass it window.top from the iframe just as well
function islocal(win){
var H=location.href,
local= H.substring(0, H.indexOf(location.pathname));
try{
win=win.document;
return win && win.URL && win.URL.indexOf(local)== 0;
}
catch(er){
return false
}
}
//test case alert(islocal(window.top))
Try this:
function parentIsSameOrigin()
{
var result = true;
if (window.parent)
{
result = Boolean
(
// more precise modifications needed here
window.this.location.href.indexOf(window.parent.location.href) == 0
);
}
return result;
}
本文标签: browserJavaScript to detect if the parent frame is of the same originStack Overflow
版权声明:本文标题:browser - JavaScript to detect if the parent frame is of the same origin? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738616178a2102920.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论