最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to feature detect if XMLHttpRequest supports responseType = "arraybuffer"? - Stack Overflow

programmeradmin2浏览0评论

I want to know if the browser supportes XMLHttpRequest.responseType = "arraybuffer". Problem is, that I can not test agains some "general" xhr2 support, since iOS 4.2 has partial xhr2 support which includes (i.e.) XMLHttpRequestUpload but not responseType = "arraybuffer".

I want to know if the browser supportes XMLHttpRequest.responseType = "arraybuffer". Problem is, that I can not test agains some "general" xhr2 support, since iOS 4.2 has partial xhr2 support which includes (i.e.) XMLHttpRequestUpload but not responseType = "arraybuffer".

Share Improve this question edited Jan 19, 2012 at 14:18 Aron Woost asked Jan 19, 2012 at 13:07 Aron WoostAron Woost 20.7k13 gold badges44 silver badges53 bronze badges 0
Add a comment  | 

6 Answers 6

Reset to default 11

I am using the following:

var supported = typeof new XMLHttpRequest().responseType === 'string';

In all browsers I tested that support this, the default value of responseType is an empty string (just like it says in the spec: http://www.w3.org/TR/XMLHttpRequest/#the-responsetype-attribute ), in browsers that don't support responseType the value of the attribute is undefined.

Checking of ArrayBuffer should be a good feature detection.

If a userAgent supports the ArrayBuffer object then it's likely it will work with XHR2

However as noted, it would be best to do a feature test and not a feature detection.

function IsArrayBufferSupported(cb){
    var xhr = new XMLHttpRequest();
    xhr.open('GET', '/', true);
    try {
       xhr.responseType = "arraybuffer";
    } catch (e){
        return cb(false);
    }
    xhr.onload = function onload() {
        if (ArrayBuffer.prototype.isPrototypeOf(this.response)) {
            return cb(true);
        }
        cb(false);
    }
    xhr.send();
}

Have you tried something like this?

if(typeof(XMLHttpRequestUpload) == "undefined"){
    //not supported
}

Edit

I think you might be stuck with somthing nasty like this

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

Set responseType to "arraybuffer" and check if it got the new value:

// call like isResponseTypeSupported('arraybuffer')
function isResponseTypeSupported(responseType) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', '/');
    try {
        xhr.responseType = responseType;
    } catch (e) {
        return false;
    }
    return xhr.responseType === responseType;
}

Using Modernizr this is covered under Modernizr.xhr2. Following up on the comments regarding partial support Modernizr.dataview might be even more accurate.

(function(modernizr, ns){
    ns.isSupported = (function(){
        return modernizr.xhr2 && modernizr.dataview;
    });

    return ns;
}(window.Modernizr, window.NameSpace || {}));

I expect both features to be supported or not.

If you just want to detect if the "arraybuffer" response is supported, just check if it's in the global object. If you want to detect other features just assign the XHR().responseType until the browser empty it "" or throw a error.

function isAjaxResponseSupported(type) {
    var xhr = new XMLHttpRequest;

    /* Check if .responseType is supported first */
    if (typeof xhr.responseType === 'string') {

        /* Some browsers throw error for invalid .responseType */
        try {
            xhr.responseType = type;
            // If they don't,
            // check if .responseType is equal to @type.
            return xhr.responseType === type;
        } catch (e) {
            return false;
        }

    ; else return false;        
}
发布评论

评论列表(0)

  1. 暂无评论