1

How to check if the class URL() is supported in the current browser? Based on the docs it's not supported in IE

I want to use it to get a domain out of a string like that:

var text = ...
var domain = new URL(text).host;
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
  • `if (window.URL) { var domain = new URL(text).host }` – Fabio Antunes Mar 09 '17 at 15:52
  • Possible duplicate of [JavaScript check if variable exists (is defined/initialized)](http://stackoverflow.com/questions/5113374/javascript-check-if-variable-exists-is-defined-initialized) – RyanZim Mar 09 '17 at 15:54

3 Answers3

5

You could do a feature check with

if ("URL" in window)

However this won't validate whether the functionality is correct. you might want to consider adding a polyfill.

Note that IE/Edge seem to really make built in constructors as objects, meaning typeof Ctor === "object" is true in those browsers. So if they add support in Edge for it, the checks for "function" will be invalid.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
2

You can't do this with complete reliability. You could come close by testing if URL exists and is a function:

if (typeof URL !== "function") {
     // It is not supported
}

You could then perform further tests to see how much it looks like a duck:

function URL_is_supported() {
    if (typeof URL !== "function") {
        return false;
    }
    // Beware: You're calling the function here, so it it isn't the expected URL function it might have undesired side effects
    var url = new URL("http://example.com");
    if (url.hostname !== "example.com") {
        return false;
    }
    // and whatever other tests you wanted to do before you're convinced
    return true;
}
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

to check if anything is supported on a root level (window) just try to access it on a condition level. E.G.

(window.URL) OR JUST (typeof URL === "function")

var a = window.URL ? window.URL(text).host : ....

also remembering that the fact of window has a "URL" property doesn't mean that it is a class/function and that it is what you expect so the best approach would be check using the typeof version which at least guarantee that it is a function

The closest you can get to check if URL is truly supported is checking its prototype and static functions

function isURLSupported(){
    if(typeof window.URL!=="function" || typeof URL.createObjectURL !== "function" || typeof URL.revokeObjectURL !== "function"){
        return false;
    }
    var oURLprototype= ["host","hostname","href","origin","password","pathname","port","protocol","search","searchParams","username"];
    for(var i=0;i<oURLprototype.length;i++){
        if(URL.prototype.hasOwnProperty(oURLprototype[i])===undefined){
            return false;
        }
    }
    return true;
}

For those who will support implementations that have classes as type Object not Function --Is function credit to https://stackoverflow.com/a/7356528/835753

function isFunction(functionToCheck) {
    var getType = {};
    return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
}
function isURLSupported(){
    if(!isFunction(window.URL) || !isFunction(URL.createObjectURL) || !isFunction(URL.revokeObjectURL)){
        return false;
    }
    var oURLprototype= ["host","hostname","href","origin","password","pathname","port","protocol","search","searchParams","username"];
    for(var i=0;i<oURLprototype.length;i++){
        if(URL.prototype.hasOwnProperty(oURLprototype[i])===undefined){
            return false;
        }
    }
    return true;
}
Community
  • 1
  • 1
Guilherme Ferreira
  • 2,209
  • 21
  • 23