Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SCRIPT5009: 'URLSearchParams' is undefined in IE 11

Tags:

jquery

I'm trying to execute the URLSearchParams but I get an error on IE 11 since it is not supported there. It's working perfectly in Chrome and Firefox.

How can I get the equivalent functionality in IE 11? I am executing the code in Laravel 5.4.

Here is the line of code which I'm trying to execute.

var urlParams = new URLSearchParams(window.location.search);

Error:

SCRIPT5009: 'URLSearchParams' is undefined

like image 844
Narayan Adhikari Avatar asked Aug 18 '17 14:08

Narayan Adhikari


2 Answers

Got a solution by replacing the code with the following:

$.urlParam = function(name){
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (results == null){
       return null;
    }
    else {
       return decodeURI(results[1]) || 0;
    }
}

So for example "example.com?param1=name&param2=&id=6"

$.urlParam('param1');  // name
$.urlParam('id');      // 6
$.urlParam('param2');  // null
like image 187
Narayan Adhikari Avatar answered Nov 09 '22 01:11

Narayan Adhikari


Odhikari's answer as a (partial) polyfill:

(function (w) {

    w.URLSearchParams = w.URLSearchParams || function (searchString) {
        var self = this;
        self.searchString = searchString;
        self.get = function (name) {
            var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(self.searchString);
            if (results == null) {
                return null;
            }
            else {
                return decodeURI(results[1]) || 0;
            }
        };
    }

})(window)
like image 40
Andy Taw Avatar answered Nov 08 '22 23:11

Andy Taw