![](/img/trans.png)
[英]Internet Explorer doesn't execute jQuery.load (Firefox does)
[英]jQuery.load(), mixed HTTP/HTTPS and Internet Explorer
我正在尝试使用jQuery.load('https://someurl.com .someClass')
在远程HTML页面中jQuery.load('https://someurl.com .someClass')
。 执行加载的页面在HTTPS上; 远程页面可以同时使用HTTP和HTTPS。 一切在合理的浏览器中都可以正常运行,但是IE抛出了HTTP / HTTPS混合内容安全警告-远程页面具有HTTP链接包含的CSS和JS,即使当请求为HTTPS时。 如何在不触发警告的情况下成功在IE中提取混合内容文件的任何线索? 不能修改远程页面。
编辑
为了清楚起见,我正在尝试通过HTTPS加载远程文件。 该文件包含指向HTTP资源(img,css,js)的链接; 因为我为.load()
提供了选择器,所以合理的浏览器不会尝试解析和执行文档。 IE浏览器。
您无法绕开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($){
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
yesIKnowThisIsFragile
的callback
和params
参数,并添加yesIKnowThisIsFragile
参数作为微妙的提醒。
如果安全页面加载了任何非安全资源,它将引发警告。 解决该问题的唯一方法是从https加载所有内容。
甚至其他浏览器也应该在某处显示警告(可能在FF地址的左侧?)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.