您无法绕过 IE 中的混合内容警告。如果远程资源可通过 HTTP 和 HTTPS 获得,您可以确保您的协议匹配 jQuery.load(location.protocol + '//someurl.com .someClass')
根据远程页面中混合内容的问题进行了更新:
jQuery.load 将整个 responseText 加载到 documentFragment 中,然后再拉出选择器指示的相应部分(请参阅jQuery 1.4.4 ajax.js)。整个远程页面被解析为 HTML 并且必须通过浏览器的安全流程;在许多方面,通过确保所有协议匹配和/或仅在您需要的情况下仅返回片段来确保响应“干净”更简单。
如果您不修改其他资源,这将更加健壮,您需要在远程资源被替换时将所有出现的 HTTP 替换为 HTTPS(反之亦然)仍然只是一个字符串。这是一个脆弱的 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');
此函数省略了$.load 的callback 和params 参数,并添加了yesIKnowThisIsFragile 参数作为一个微妙的提醒。