关注这个问题:
jQuery AJAX upload progress for large text fields
如何使其与旧版浏览器兼容?
正如您在上面的问题中所看到的,我依赖于XHR和进展事件。现在对于旧版浏览器,我需要检测它们是否不具备其中一个,所以我可以跳过进度条并仍然可以发布我的AJAX帖子。
我认为它可以像这样工作:
$.ajax({
xhr: function() {
var xhr = $.ajaxSettings.xhr();
if (xhr instanceof window.XMLHttpRequest) {
xhr.addEventListener('progress', function(event) {
if (event.lengthComputable) {
progressPercent = Math.round(event.loaded/event.total*100)+'%';
$loader.width(progressPercent);
}
}, false);
}else{
alert('XHR is no instance of window.XMLHttpRequest');
}
return xhr;
},
type: "POST",
...
但是我不知道是否保存,或者我还有什么需要检查。
谢谢!
答案 0 :(得分:2)
对于接近完全安全的东西,你可以使用try / catch / finally结构:
$.ajax({
xhr: function() {
var xhr = $.ajaxSettings.xhr();
try {
xhr.addEventListener('progress', function(event) {
if (event.lengthComputable) {
$loader.width(Math.round(event.loaded / event.total * 100) + '%');
}
}, false);
}
catch(err) {
//Progess not available
//Take appropriate action - eg hide the progress thermometer
$loader.hide();
}
finally {
return xhr;
}
},
type: "POST",
...
}):