检查窗口父级是否是相同的域 [英] Check if window parent is same domain

查看:182
本文介绍了检查窗口父级是否是相同的域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我创建的应用程序将添加到iframe中的不同网页。但有时它会在我自己的域,有时在一些小精灵。
当它托管在我自己的域上时,我想能够在框架外调用函数。

My problem is that I'm creating an application that will be added to different pages in an iframe. But sometimes it will be on my own domain and sometimes on some one elses. And when it is hosted on my own domain I want to be able to call functions outside the frame.

可以检查父窗口是否在同一个域(用javascript / jQuery)?
目前,当我没有在我自己的网站上托管时,当我尝试访问框架之外的某个时候,我得到这个丑陋的错误:
错误:权限被拒绝访问属性'document'

Is it possible to check if the parent window is on the same domain (with javascript/jQuery)? At the moment I get this ugly error when trying to access somethin outside my frame when not hosted on my own site: "Error: Permission denied to access property 'document'"

要这样做:

if(window.parent is accessible) {
    // Do special stuff
} else {
   // Do nothing
}


推荐答案

您可以使用:

try{
    parent.document;
    // accessible
}catch(e){
    // not accessible
}

这篇关于检查窗口父级是否是相同的域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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