获取顶部框架的URL

时间:2011-02-04 21:54:20

标签: javascript jquery

在facebook应用程序中,我需要检查顶部框架(主窗口)URL是什么,并相应地显示内容。

我尝试使用以下内容:

if (top.location.toString().toLowerCase().indexOf("facebook.com") <0) { ... }

如果页面不在iframe中,但是当页面在iframe中加载时(与用作facebook应用程序时相同),代码生成

  

“未捕获的TypeError:属性   对象#的'toString'不是   功能”。

有什么方法可以修复这段代码(跨浏览器兼容性 - 可能是jQuery)?

谢谢!

乔尔

6 个答案:

答案 0 :(得分:9)

确实,交叉来源问题会阻止您访问此顶部窗口位置。但是,如果您只想要iframe的父窗口位置,则可以通过document.referrer字符串获取该位置。

在你的iframe中你会抓住网址:

var parentURL = document.referrer

https://developer.mozilla.org/en-US/docs/Web/API/document.referrer

我已经在我自己的iframe应用中成功使用了这个。此外,请注意,如果您在iframe中导航,引荐来源将会更改。

Nicholas Zakas在他的博客上写了一篇文章: http://www.nczonline.net/blog/2013/04/16/getting-the-url-of-an-iframes-parent/

答案 1 :(得分:5)

您遇到的问题是不允许您跨不同的文档域访问top.location

这是浏览器内置的安全功能。

Read up on XSS以及为什么采取安全预防措施:)

通过阅读the same origin policy

,您还可以学到很多东西

答案 2 :(得分:3)

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
}

答案 3 :(得分:1)

这可行:

if (self!=top && document.referrer.toLowerCase().indexOf("facebook.com") <0) { ... }

...只要你不在框架内导航。

但这不是一个好的解决方案^^

答案 4 :(得分:0)

你试过这个:

var location = top.location.toString().toLowerCase();
if (location.indexOf("facebook.com") <0) { ... }

答案 5 :(得分:0)

如果您需要尽可能多的有关首页位置的信息

function getTopLinkInfo() {
  var topLinkInfo = {};

  try {
    // Only for same origins
    topLinkInfo.topHref = top.location.href;
  }
  // Security exception: different origins
  catch (error) {
    try {
      var ancestorOrigins = window.location.ancestorOrigins;
      // Firefox doesn't support window.location.ancestorOrigins
      if (ancestorOrigins) {
        topLinkInfo.parentsDomains = [];
        for (var i = 0; i < ancestorOrigins.length; i++) {
          topLinkInfo.parentsDomains.unshift(ancestorOrigins[i]);
        }
      }

      // Sometimes referrer refers to the parent URL (but not always,
      // e.g. after iframe redirects).
      var bottomW = window;
      var topW = window.parent;
      topLinkInfo.parentsReferrers = [];
      // In case of empty referrers
      topLinkInfo.parentsHrefs = [];

      while (topW !== bottomW) {
        topLinkInfo.parentsReferrers.unshift(bottomW.document.referrer);
        topLinkInfo.parentsHrefs.unshift(bottomW.location.href);
        bottomW = bottomW.parent;
        topW = topW.parent;
      }
    } catch (error) {/* Permission denied to access a cross-origin frame */}
  }
  return topLinkInfo;
}

var result = getTopLinkInfo();
console.table(result);
console.info(result);