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

我正在尝试使用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确实如此。

您无法绕过IE中的混合内容警告。 如果远程资源可通过HTTP和HTTPS获得,则可以确保您的协议与jQuery.load(location.protocol + '//someurl.com .someClass')匹配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($){ var http = "http:", https = "https:", rscript = /)<[^<]*)*<\/script>/gi, proto = location.protocol, otherProtoUri = new RegExp("\\b" + (proto === http ? https : http) + "(//[az\\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("
") // 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');

这个函数省略了$.load yesIKnowThisIsFragilecallbackparams参数,并且添加了yesIKnowThisIsFragile参数作为一个微妙的提醒。

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

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