您无法绕过 IE 中的混合内容警告。如果远程资源可通过 HTTP 和 HTTPS 访问,您可以确保您的协议匹配jQuery.load(location.protocol + '//someurl.com .someClass')
根据远程页面中混合内容的问题进行了更新:
jQuery.load
加载整个响应文本放入 documentFragment 中,然后拉出选择器指示的适当部分(请参阅jQuery 1.4.4 ajax.js)。整个远程页面被解析为 HTML,并且必须经过浏览器的安全流程;在许多方面,通过确保所有协议匹配和/或仅返回片段(如果这就是您所需要的),可以更简单地确保响应是“干净的”。
如果您不修改其他资源,那就是更加稳健,您需要将所有出现的 HTTP 替换为 HTTPS(反之亦然),而远程资源仍然只是一个字符串。这是一个脆弱的 jQuery 插件作为该技术的示例,大部分来自jQuery 1.4.4 $.load 函数:
(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);
Usage: $('#your > .selector').protocolModifyLoad(location.protocol + '//someurl.com', 'thisIsFragile!!!', '.someClass');
该函数省略了callback
and params
的论据$.load
和yesIKnowThisIsFragile
添加参数作为微妙的提醒。