Javascript 正在获取顶部框架的URL
在facebook应用程序中,我需要检查顶部框架(主窗口)的URL,并相应地显示内容 我尝试使用以下方法:Javascript 正在获取顶部框架的URL,javascript,jquery,Javascript,Jquery,在facebook应用程序中,我需要检查顶部框架(主窗口)的URL,并相应地显示内容 我尝试使用以下方法: if (top.location.toString().toLowerCase().indexOf("facebook.com") <0) { ... } if(top.location.toString().toLowerCase().indexOf(“facebook.com”)您遇到的问题是不允许跨不同文档域访问top.location 这是浏览器内置的安全功能 以及安全预防
if (top.location.toString().toLowerCase().indexOf("facebook.com") <0) { ... }
if(top.location.toString().toLowerCase().indexOf(“facebook.com”)您遇到的问题是不允许跨不同文档域访问top.location
这是浏览器内置的安全功能
以及安全预防措施到位的原因:)
通过阅读有关的内容,您还可以学到很多东西。这可能会奏效:
if (self!=top && document.referrer.toLowerCase().indexOf("facebook.com") <0) { ... }
if(self!=top&&document.referer.toLowerCase().indexOf(“facebook.com”)跨源问题确实会阻止您访问此顶部窗口位置。但是,如果您只需要iframe的父窗口位置,则可以通过document.referer
字符串获取它
在iframe中,您可以获取url:
var parentURL=document.referer
我已经在我自己的iframe应用程序中成功地使用了它。另外,请注意,如果您在iframe中导航,推荐人将发生变化
尼古拉斯·扎卡斯在他的博客上写道:
使用Martin Jespersen建议的修复程序,我可以检查iFrame和standart top address中的地址:
//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
}
如果您需要尽可能多的有关首页位置的信息,请执行以下操作:
函数getTopLinkInfo(){
var topLinkInfo={};
试一试{
//只有相同的起源
topLinkInfo.topHref=top.location.href;
}
//安全例外:不同的起源
捕获(错误){
试一试{
var ancestorOrigins=window.location.ancestorOrigins;
//Firefox不支持window.location.ancestorOrigins
如果(取消源代码){
topLinkInfo.parentsDomains=[];
对于(变量i=0;i console.info(result);
有趣…那么,是否有任何解决方法可以知道我当前是否正在通过iframe查看页面?(查看了您关于XSS的编辑-谢谢。我知道访问顶部框架cookie可能会有问题。但是应该允许获取主机名:/)如果您不在iframewindow.top==window
中,则返回true。您无法在顶部位置获取,但可以使用document.referer字符串在父级位置获取。请参阅下面的答案。