获取顶部框架的URL [英] Getting URL of the top frame

查看:68
本文介绍了获取顶部框架的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我尝试使用以下内容:

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

如果该页面不在iframe内,则效果很好,但是当该页面在iframe中加载时(如用作facebook应用程序时一样),代码会生成

未捕获的TypeError:属性 对象#的'toString'不是 功能".

有什么办法可以修复此代码(具有跨浏览器的兼容性-也许与jQuery兼容)?

谢谢!

乔尔

解决方案

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

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

阅读XSS 以及为什么要采取安全预防措施:)

您还可以通过阅读同一个原产地政策

来学到很多东西.

Inside a facebook application I need to check what is the top frame (the main window) URL, and show content accordingly.

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

"Uncaught TypeError: Property 'toString' of object # is not a function".

Is there any way I can fix this code (with cross-browser compatibility - maybe with jQuery)?

Thanks!

Joel

解决方案

The problem you are having that you are not allowed to access top.location across different document domains.

This is a security feature built in to browsers.

Read up on XSS and why the security precautions are in place :)

You can also learn a great deal by reading about the same origin policy

这篇关于获取顶部框架的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆