Alternative for URLSearchParams

IE doesn’t support “URLSearchParams”. So sometimes your script will not work in IEs.

So the alternative for the “URLSearchParams” is as below.

window.getUrlParameter = function( keyword, query_string ) {
    keyword = keyword.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    var regex = new RegExp( keyword + '=([^&#]*)');
    var results = regex.exec( query_string );
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};

 

We can use the above function like this:

Suppose you have query string like “abc=test&action=wow” and you want to search values of the particlular query string. So you can use it something like below:

var query_string = 'abc=test&action=wow';

getUrlParameter( 'abc', query_string ); // You will get value "test"

getUrlParameter( 'action', query_string ); // You will get value "wow"

Leave a Reply