Check valid XMLHttpRequest responseTypes

You can check whether a responseType is supported by setting it, then checking the responseType
is what you just set it to, and wrap it all within a try catch. But that seems pretty messy to me. Is there no other way to check what values are valid? Or do browsers not expose the responseTypes they support? I was thinking there should be a static property you can check, e.g. 'json' in XMLHttpRequest.responseTypes

Well, this is “standard procedure”. You try something and catch an error. If you don’t you are good to go! According to the specs (and other sources), there is a bunch of possible string values and only Can I Use actually has info on what value is supported where.

1 Like

Feature detection in JavaScript is often a messy task, if you post your example of how you’re checking supported response types we can look at it. But often it does require setting properties on objects and seeing if those values stick as you describe.

1 Like

Thanks for the replies. It sounds like I’ve just been lucky with the features I’ve had to test for so far. I’m used to just checking if a feature / property exists, not checking if it can be set to a specific value.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.