【问题标题】:Programmatically detect Mixed Content blocking in Chrome以编程方式检测 Chrome 中的混合内容阻止
【发布时间】:2015-01-12 14:44:03
【问题描述】:

有谁知道是否可以在 JavaScript 中检测到 Chrome 由于混合内容限制而阻止了内容?我看到控制台中有一条错误消息,但这似乎不是异常,因为使用 try/catch 不会捕获任何东西..

那么问题来了,如何检测到内容被屏蔽了?

谢谢!

【问题讨论】:

  • 您是否尝试过提供来自不安全域的 JavaScript 文件,该文件在 window 对象中设置了一个全局变量?从逻辑上讲,如果脚本被阻止,它就不会执行。
  • 您好,感谢您的建议,但很遗憾,我认为这不是我们的选择。
  • 如果在 DOM 结构中包含 <img src="http://...">,则使用 JavaScript(处于 DOM 就绪状态)获取其宽度,如果为 0,则无法加载。 TMTOWTDI
  • 另外,这听起来像是X-Y problem——你最终想用这些信息完成什么?
  • 我们的网站在 https 上运行,并且我们有 iframe 有时会从 http 资源加载页面。在这种情况下,我想检测 Chrome 是否阻止了内容并向用户显示消息。只是为了摆脱它:不,我们不能将 http 资源转换为 https,这是我们无法控制的。 :)

标签: javascript google-chrome


【解决方案1】:

我有一个类似的场景,我盲目地嵌入了 HTTPHTTPS 域上的 iframes。我发现了一个有效的解决方法,假设如下:

  • 您的网站同时拥有 HTTPHTTPS 版本。
  • 你有 jQuery 可用

我已经对代码进行了注释,以便于理解:

<script type="text/javascript">
    $(function () { /* when DOM is fully loaded */
        if ("https:" == window.location.protocol)
        { /* we are on the SSL (HTTPS) version of our website */
            $.each($("iframe"), function (k, iframe)
            { /* iterate through all iframes */
                if ("http:" == $(iframe).attr('src').substring(0, 5))
                { /* there's at least an iframe with non-SSL (HTTP) content (that will be blocked by the browser) */
                    /* lets redirect user to the non-SSL (HTTP) version of the website */
                    window.location = window.location.href.replace(/^https:\/\//ig, "http://");
                    return false;
                }
            })
        }
    });
</script>

【讨论】:

  • 感谢您的回答。不幸的是,我们不能使用这种方法,因为我们只在 HTTPS 中运行我们的网站。
猜你喜欢
  • 1970-01-01
  • 2016-07-20
  • 2016-11-05
  • 2018-10-12
  • 1970-01-01
  • 2019-06-09
  • 1970-01-01
  • 2014-01-03
  • 1970-01-01
相关资源
最近更新 更多