javascript 获取顶部框架的 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4903351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting URL of the top frame
提问by Joel
Inside a facebook application I need to check what is the top frame (the main window) URL, and show content accordingly.
在 facebook 应用程序中,我需要检查顶部框架(主窗口)URL 是什么,并相应地显示内容。
I tried using the following:
我尝试使用以下方法:
if (top.location.toString().toLowerCase().indexOf("facebook.com") <0) { ... }
Which works well if the page is not inside an iframe, but when the page is loaded within an iframe (as it does when used as facebook application) the code generates
如果页面不在 iframe 中,则效果很好,但是当页面在 iframe 中加载时(就像用作 facebook 应用程序时那样),代码会生成
"Uncaught TypeError: Property 'toString' of object # is not a function".
“未捕获的类型错误:对象 # 的属性 'toString' 不是函数”。
Is there any way I can fix this code (with cross-browser compatibility - maybe with jQuery)?
有什么方法可以修复此代码(具有跨浏览器兼容性 - 也许使用 jQuery)?
Thanks!
谢谢!
Joel
乔尔
回答by mcanfield
It is true that cross origin concerns will prevent you from accessing this top window location. However, if you just want the parent window location of the iframe you can get at it via the document.referrerstring.
确实,跨源问题会阻止您访问此顶部窗口位置。但是,如果您只想要 iframe 的父窗口位置,则可以通过document.referrer字符串获取它。
Within your iframe you'd grab the url:
在您的 iframe 中,您将获取网址:
var parentURL = document.referrer
var parentURL = document.referrer
https://developer.mozilla.org/en-US/docs/Web/API/document.referrer
https://developer.mozilla.org/en-US/docs/Web/API/document.referrer
I've used this successfully in my own iframe apps. Also, be aware that if you navigate within your iframe the referrer will change.
我已经在我自己的 iframe 应用程序中成功地使用了它。另外,请注意,如果您在 iframe 中导航,引荐来源网址将发生变化。
Nicholas Zakas has a write-up on his blog: http://www.nczonline.net/blog/2013/04/16/getting-the-url-of-an-iframes-parent/
Nicholas Zakas 在他的博客上写了一篇文章:http: //www.nczonline.net/blog/2013/04/16/getting-the-url-of-an-iframes-parent/
回答by Martin Jespersen
The problem you are having that you are not allowed to access top.locationacross different document domains.
您遇到的问题是您不能top.location跨不同的文档域进行访问。
This is a security feature built in to browsers.
这是浏览器内置的安全功能。
Read up on XSSand why the security precautions are in place :)
阅读 XSS以及为什么要采取安全预防措施:)
You can also learn a great deal by reading about the same origin policy
您还可以通过阅读有关同源策略的信息来学到很多东西
回答by Nick Bristol
With Martin Jespersen adviced fix, I could check address in iFrame and standart top address:
在 Martin Jespersen 建议修复的情况下,我可以检查 iFrame 中的地址和标准顶级地址:
//this is fix for IE
if (!window.location.origin) {
window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
}
//and here we will get object of address
var urls = (window.location != window.parent.location) ? document.referrer: document.location;
//Martins adviced fix for checking if You are not in iFrame
if (window.top === window) {
urls = urls.origin;
}
//and now indexOf works in both ways - for iFrame and standart top address
if (urls.indexOf("facebook.com") != -1 ) {
//do stuff
}
回答by Dr.Molle
This could work:
这可以工作:
if (self!=top && document.referrer.toLowerCase().indexOf("facebook.com") <0) { ... }
...as long as you don't navigate inside the frame.
...只要您不在框架内导航。
But it's not really a good solution ^^
但这不是一个很好的解决方案^^
回答by Yoram de Langen
Did you try this:
你试过这个吗:
var location = top.location.toString().toLowerCase();
if (location.indexOf("facebook.com") <0) { ... }

