错误:权限被拒绝访问属性'$' - iFrame [英] Error: Permission denied to access property '$' - iFrame

查看:395
本文介绍了错误:权限被拒绝访问属性'$' - iFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Bootstrap模式中的iFrame时遇到问题。它是一个新闻系统,我从另一个子域的iFrame加载新闻内容。一切工作正常,如果用户点击任何新闻项目中的图像,我想在父窗口中打开灯箱(Fancybox)。这通常不是问题,我使用的是这样的:

  //所以我知道它的一个Popup /有一个iFrame class Popup就是当它在BS Modal中打开时) - 否则不适合我的新闻大小并在那里显示文章。点击(功能(e){
e.preventDefault();

var link =
$ b $('a [rel =lightbox]' $(this).attr('href');

if($(this).closest('body')。hasClass('popup')){
parent。$。 fancybox({

//这是没有任何弹出窗口

$ .fancybox({
href:link,
...

我的图片位于另一个域名 - 比如static.site.com。当生病时转到news.site.com - 打开一个Popup(现在它来自同一个域) - 然后单击一个图像 - 一切都很好,

 父项。$ 

可以正常工作。

但是,主网站(www.site.com),当我打开Modal(来自news.site.com的内容),然后点击图片(static.site.com)时,出现以下错误:

 错误:权限被拒绝访问属性'$'

我已经允许PHP标题从另一个子域加载内容 - 但生病得到这个错误。



我是否需要使用JSONP(将页面的内容加载为HTML?) - 或者是否存在另一个更简单的解决方案?

解决方案

您需要将两个域都设置为在JavaScript中相同的顶级域名


页面可能会因某些限制而改变其原点。脚本可以将document.domain的值设置为当前域的子集。如果是这样,则较短的域用于随后的原点检查。

所以如果你有 static.example.com news.example.com 在尝试任何跨帧访问之前,您应该包含此JavaScript以运行: $ b

  document.domain ='example.com'; 

您需要在这两个窗口中运行此代码。


I have a problem using an iFrame in an Bootstrap Modal. Its a News System, and i load the News Content from an iFrame from another Subdomain. Everything works fine, if a user clicks on an Image from the any news item, i want to open a Lightbox (Fancybox) in the Parent Window. Thats normally not the problem, i am using something like this:

// so i know its an Popup / has an iFrame (class Popup is when it is opened in an BS Modal) - otherwise ill go to my news size and show the article there.

$('a[rel="lightbox"]').click(function(e) {
    e.preventDefault();

    var link = $(this).attr('href');

    if ($(this).closest('body').hasClass('popup')) {
        parent.$.fancybox({

// this is without any popup

    $.fancybox({
        href: link,
        ...

My Images are on another Domain - like static.site.com. When ill go to news.site.com - and open an Popup (now its from the same domain) - and click an image - everything is fine, the

 parent.$ 

works fine.

But, from the main site (www.site.com) when i am opening the Modal (Contents from news.site.com) and then click on the image (static.site.com) ill the the following error:

Error: Permission denied to access property '$' 

I have already allowed the PHP Header to load content from another subdomain - but ill get still this error.

Do i need to use JSONP (to load the Content of the Page as HTML?) - or is there another, simpler Solution?

解决方案

You need to set both domains to the same top level domain in JavaScript:

A page may change its own origin with some limitations. A script can set the value of document.domain to a subset of the current domain. If it does so, the shorter domain is used for subsequent origin checks.

So if you have static.example.com and news.example.com you should include this JavaScript to run before you try any cross-frame access:

document.domain = 'example.com';

You will need to run this code in both windows.

这篇关于错误:权限被拒绝访问属性'$' - iFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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