Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE11 - Object doesn't support this action - URL object - workaround

I'm trying to convert the URL string to a URL object using this command:

var newUrl = new URL("http://www.testsite.com?name=abc&class=123");

It works for Chrome and Firefox, but not in IE11. Is there a known workaround for this?

Thanks.

like image 292
fractal5 Avatar asked Jan 30 '18 15:01

fractal5


1 Answers

No, the URL object isn't available in IE https://developer.mozilla.org/en-US/docs/Web/API/URL

You can however use some small library to fake it http://blog.stevenlevithan.com/archives/parseuri

// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License
// http://blog.stevenlevithan.com/archives/parseuri

function parseUri (str) {
    var o   = parseUri.options,
        m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
        uri = {},
        i   = 14;

    while (i--) uri[o.key[i]] = m[i] || "";

    uri[o.q.name] = {};
    uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
        if ($1) uri[o.q.name][$1] = $2;
    });

    return uri;
};

parseUri.options = {
    strictMode: false,
    key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
    q:   {
        name:   "queryKey",
        parser: /(?:^|&)([^&=]*)=?([^&]*)/g
    },
    parser: {
        strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
        loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
    }
};

Usage

parseUri.options.strictMode = true;
like image 89
Eric Herlitz Avatar answered Oct 11 '22 02:10

Eric Herlitz