【问题标题】:force target="_self" on dynamic external cross domain in iframe在 iframe 中的动态外部跨域上强制 target="_self"
【发布时间】:2018-10-15 23:25:09
【问题描述】:

我在 chrome 上使用 iframe 有一个处于信息亭模式的站点,我在其中打开了许多外部站点。每个站点都有自己的链接,target="_blank"。我无法控制这些外部链接。 我只想在 iframe 中打开所有这些外部链接,或者我需要为这些 target=_blank 链接发布一些消息。我无法在自助服务终端上打开新窗口。到目前为止我已经搜索过,发现它必须是一些服务器端编码。 Javascript 在这方面没有任何帮助。

我的 iframe 代码:

  navigateToExternal:function(href)
    {
        Engine.ui.openMenu();
        Engine.ui.exit();
        Engine.ui.mostRecentSection = "external";

        $("a[data-navsection], a[href]").removeClass("disabled");
        $("a[href='" + href + "']").addClass("disabled");

        $(".footer").hide();

       var sandbox = "sandbox=\"allow-forms allow-scripts allow-same-origin\"";


         $("#content-container").append("<iframe id=\"contentFrame\" src=" + href + "\"" + sandbox + "></iframe>");
    },

任何帮助或建议都会非常棒。

提前致谢!!

【问题讨论】:

    标签: php iframe


    【解决方案1】:

    不能,父页面无法访问跨站iframe,iframe也无法访问跨站父页面。

    这属于同源政策,无法在客户端解决。您可以在服务器上抓取这些站点并将它们呈现为您自己的,但我不确定这会有多有效。

    【讨论】:

    • 感谢您的回复!!有没有办法在点击这些外部链接的 target = "_blank" 时显示一些消息?
    • 否,因为您无法捕获该点击事件,它会在该 iframe 的文档处停止。
    • 非常感谢您的所有回复。我得到了解决这个问题,并想分享。对于 Mac,我们可以传递命令行参数,打开终端并运行:$ open -a Google\ Chrome --args --disable-web-security,它将禁用 Same-Origin-Policy,然后我们可以在跨站点 iframe 上进行所需的更改。
    【解决方案2】:

    如果你在 Chrome 上运行,你可以试试 html5 iframe sandbox 属性。

    <iframe sandbox="allow-scripts allow-forms" src="..."></iframe>
    

    More Reading

    【讨论】:

      【解决方案3】:

      我解决了这个问题并想分享。我们可以传递一个命令行参数对于 Mac,打开终端并运行:

       $ open -a Google\ Chrome --args --disable-web-security 
      

      它将禁用同源策略,然后我们可以在跨站点 iframe 上进行所需的更改。

      【讨论】:

        猜你喜欢
        • 2023-04-06
        • 1970-01-01
        • 2023-03-29
        • 2016-01-12
        • 1970-01-01
        • 2013-03-24
        • 2021-02-15
        • 1970-01-01
        相关资源
        最近更新 更多