以编程方式检测Chrome中的混合内容阻止

时间:2015-01-12 14:44:03

标签: javascript google-chrome

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

所以问题是,如何检测内容已被阻止?

谢谢!

1 个答案:

答案 0 :(得分:0)

我有类似的情况,我盲目地嵌入 iframes ,可以在 HTTP HTTPS 域上。假设有以下内容,我发现了一个可行的脏方法:

  • 您可以使用 HTTP HTTPS 版本的网站。
  • 您有 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>
相关问题