【问题标题】:Error: Permission denied to access property '$' - iFrame错误:访问属性“$”的权限被拒绝 - iFrame
【发布时间】:2015-02-26 08:36:39
【问题描述】:

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

// 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,
        ...

我的图片位于另一个域 - 例如 static.site.com。当生病去 news.site.com - 并打开一个弹出窗口(现在它来自同一个域) - 并单击图像 - 一切都很好,

 parent.$ 

工作正常。

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

Error: Permission denied to access property '$' 

我已经允许 PHP 标头从另一个子域加载内容 - 但仍然会出现此错误。

我是否需要使用 JSONP(将页面内容加载为 HTML?) - 还是有其他更简单的解决方案?

【问题讨论】:

    标签: html iframe modal-dialog lightbox


    【解决方案1】:

    您需要在 JavaScript 中将两个域都设置为 same top level domain

    页面可能会更改其自身的来源,但有一些限制。脚本可以将 document.domain 的值设置为当前域的子集。如果这样做,则较短的域将用于后续的来源检查。

    因此,如果您有 static.example.comnews.example.com,则应在尝试任何跨框架访问之前包含此 JavaScript 以运行:

    document.domain = 'example.com';
    

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

    【讨论】:

    • 谢谢,我会试试的。
    猜你喜欢
    • 2015-03-18
    • 2011-10-05
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    相关资源
    最近更新 更多