如何检测XMLHttpRequest是否支持responseType ="arraybuffer"?

Aro*_*ost 8 html javascript html5 w3c

我想知道浏览器是否支持XMLHttpRequest.responseType = "arraybuffer".问题是,我无法再次测试一些"通用"xhr2支持,因为iOS 4.2具有部分xhr2支持,包括(即)XMLHttpRequestUpload但不支持responseType = "arraybuffer".

bwi*_*els 10

我使用以下内容:

var supported = typeof new XMLHttpRequest().responseType === 'string';
Run Code Online (Sandbox Code Playgroud)

在我测试的所有支持它的浏览器中,responseType的默认值是一个空字符串(就像它在规范中所说:http://www.w3.org/TR/XMLHttpRequest/#the-responsetype-attribute),不支持responseType的浏览器未定义属性的值.

  • 这似乎是一个很好的考验.对于Android 2.3,我得到"未定义",对于Android 4.x我得到"字符串".这与http://caniuse.com/xhr2非常吻合(如iOS 4.2,Android 2.3似乎有XMLHttpRequestUpload支持,或者至少`typeof(XMLHttpRequestUpload)`返回"function",而不是"undefined") (2认同)

Sam*_*lgh 0

你尝试过这样的事情吗?

if(typeof(XMLHttpRequestUpload) == "undefined"){
    //not supported
}
Run Code Online (Sandbox Code Playgroud)

编辑

我想你可能会被这样讨厌的事情困住

function IsArrayBufferSupported(){
    var xhr = new XMLHttpRequest();
    xhr.open('GET', '/', true);
    try{
       xhr.responseType = "arraybuffer";
       return true;
    }catch(e){return false;}
}
Run Code Online (Sandbox Code Playgroud)