jQuery.load(),混合HTTP / HTTPS和Internet Explorer

时间:2010-12-10 01:01:13

标签: internet-explorer jquery https

我正在尝试使用jQuery.load('https://someurl.com .someClass')加载远程HTML页面。执行加载的页面是HTTPS;远程页面可用作HTTP和HTTPS。在合理的浏览器中一切正常,但IE正在抛出混合的HTTP / HTTPS内容安全警告 - 即使以HTTPS身份请求,远程页面也包含HTTP链接的CSS和JS。有关如何在IE中成功提取混合内容文件而不触发警告的任何线索?修改远程页面不是一种选择。

修改

要清楚,我正在尝试通过HTTPS加载远程文件。该文件包含HTTP资源的链接(img,css,js);因为我正在为.load()提供一个选择器,合理的浏览器不会尝试解析&执行文件; IE确实。

2 个答案:

答案 0 :(得分:4)

您无法绕过IE中的混合内容警告。如果远程资源可通过HTTP和HTTPS使用,则可以确保您的协议与jQuery.load(location.protocol + '//someurl.com .someClass')

匹配

根据远程页面中混合内容的问题进行了更新:

jQuery.load整个responseText 加载到documentFragment中,然后拉出选择器指示的相应部分(请参阅jQuery 1.4.4 ajax.js)。整个远程页面被解析为HTML,必须通过浏览器的安全过程;在许多方面,通过确保所有协议匹配和/或仅返回片段(如果您需要的话)来确保响应“干净”更简单。

如果您不修改其他资源(更强大),则需要使用HTTPS替换所有出现的HTTP(反之亦然),而远程资源是还是只是一个字符串。这是一个脆弱的jQuery插件,作为这种技术的一个例子,大多是从jQuery 1.4.4 $.load function中删除的:

(function($){
    var http = "http:",
        https = "https:",
        rscript = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
        proto = location.protocol,
        otherProtoUri = new RegExp("\\b" + (proto === http ? https : http) + "(//[a-z\\d.-]+)", "gi");

    $.fn.protocolModifyLoad = function(url, yesIKnowThisIsFragile, selector) {
        var self = this;

        if ( !this.length || !yesIKnowThisIsFragile) {
            return this;
        }

        $.ajax({
            url: url,
            type: "GET",
            dataType: "html",
            complete: function( res, status ) {
                // If successful, inject the HTML into all the matched elements
                if ( status === "success" || status === "notmodified" ) {
                    // Force occurences of the other protocol into the current one
                    var response = res.responseText.replace(otherProtoUri, proto + "$1");

                    // See if a selector was specified
                    self.html( selector ?
                        // Create a dummy div to hold the results
                        jQuery("<div>")
                            // inject the contents of the document in, removing the scripts
                            // to avoid any 'Permission Denied' errors in IE
                            .append(response.replace(rscript, ""))

                            // Locate the specified elements
                            .find(selector) :

                        // If not, just inject the full result
                        response);
                }
            }
        });

        return this;
    };
})(jQuery);

用法:$('#your > .selector').protocolModifyLoad(location.protocol + '//someurl.com', 'thisIsFragile!!!', '.someClass');

此函数省略了callback的{​​{1}}和params参数,并添加了$.load参数作为微妙的提醒。

答案 1 :(得分:0)

如果安全页面加载任何不安全的资源,它将抛出警告。绕过它的唯一方法是从https加载所有内容。

即使是其他浏览器也应该在某处显示警告(可能在FF的地址左侧?)

相关问题