如何检测不支持 XHR2 上传进度的浏览器
How to detect a browser that will not support XHR2 Upload progress
少数 android 浏览器(包括旧设备上的三星默认浏览器)将不支持
xhr.upload.onprogress
事件。所以我们无法在该浏览器上显示实时上传进度。
如何检测这些浏览器?
所以我可以更改我的设置以显示进度。
嗯。测试了一个非常老的 Firefox。 3.5.2.
第一个版本有 (new XMLHttpRequest).upload
属性.
当前版本Chrome:
send(1); //progress 1 of 1
send(40000000_chars); // 1,2 or more progress events.
火狐 3.5.2:
send(1); //no progress events.
send(40000000_chars); // some progress events.
所以如果浏览器 sending 时间不大,旧浏览器上没有pregress
事件
但是带有 .loaded == .total
的 load
事件正常触发。
当前结论:对于旧浏览器,数据发送速度太快。
以下是在 FF 3.5.2 中测试到 return false
,但对于最新的 Chrome/FF/IE11 true
设置后尝试读取 onpgress
时抛出异常。
希望对您有所帮助,
function isSupported() {
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
try {
xhr.upload.onprogress = isSupported;
var x = xhr.upload.onprogress;
return true;
} catch(e) {
return false;
}
}
简单:
var xhr = new XMLHttpRequest();
if ( xhr && ('upload' in xhr) && ('onprogress' in xhr.upload) ) // attach event...
所以如果你想要一个函数:
function supportAjaxUploadProgressEvents() {
var xhr = new XMLHttpRequest();
return !! (xhr && ('upload' in xhr) && ('onprogress' in xhr.upload));
}
少数 android 浏览器(包括旧设备上的三星默认浏览器)将不支持
xhr.upload.onprogress
事件。所以我们无法在该浏览器上显示实时上传进度。
如何检测这些浏览器? 所以我可以更改我的设置以显示进度。
嗯。测试了一个非常老的 Firefox。 3.5.2.
第一个版本有 (new XMLHttpRequest).upload
属性.
当前版本Chrome:
send(1); //progress 1 of 1 send(40000000_chars); // 1,2 or more progress events.
火狐 3.5.2:
send(1); //no progress events. send(40000000_chars); // some progress events.
所以如果浏览器 sending 时间不大,旧浏览器上没有pregress
事件
但是带有 .loaded == .total
的 load
事件正常触发。
当前结论:对于旧浏览器,数据发送速度太快。
以下是在 FF 3.5.2 中测试到 return false
,但对于最新的 Chrome/FF/IE11 true
设置后尝试读取 onpgress
时抛出异常。
希望对您有所帮助,
function isSupported() {
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
try {
xhr.upload.onprogress = isSupported;
var x = xhr.upload.onprogress;
return true;
} catch(e) {
return false;
}
}
简单:
var xhr = new XMLHttpRequest();
if ( xhr && ('upload' in xhr) && ('onprogress' in xhr.upload) ) // attach event...
所以如果你想要一个函数:
function supportAjaxUploadProgressEvents() {
var xhr = new XMLHttpRequest();
return !! (xhr && ('upload' in xhr) && ('onprogress' in xhr.upload));
}