js batch get browser parameters

Keywords: Javascript

Previously, getting browser parameters was like this

function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)
        return  unescape(r[2]);
      return null;
}

// Calling method
alert(GetQueryString("Parameter name 1"));

This method is suitable for obtaining a single parameter, but it will be very troublesome when there are more than one parameter. Is there any way to put all the browser parameters into one object? So I wrote the following method.

function getParameters() {                    //Get address bar parameter does not include hash value
            var search = decodeURI(location.search);//Prevent Chinese miscoding
            search = search.replace('?', '');
            var searray = search.split("&");
            var obj = {}
            searray.map(function (item) {
                var sarry = item.split('=');
                obj[sarry[0]] = sarry[1];
            })
            return obj;
}

var para=getParameters();

Is it very simple and convenient

Well, the first one has advantages when acquiring a single one, and the first one has advantages when acquiring all, so can we integrate it?

All right, just do it

function getParameters(obj) {//Get address bar parameter does not include hash value
            var search = decodeURI(location.search).replace('?', '');//Prevent Chinese miscoding
            if(typeof obj=="string"){
                var reg = new RegExp("(^|&)"+ obj +"=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if(r!=null)return  unescape(r[2]); return null;
            }else if(typeof obj=="undefined"){
                var searray = search.split("&");
                var obj = {}
                searray.map(function (item) {
                    var sarry = item.split('=');
                    obj[sarry[0]] = sarry[1];
                })
                return obj;
            }

}

// getParameters();
//  getParameters('tn');

If you are not satisfied, are you thinking that if I don't want to get one or all, I want to get several of them?

Who let me love you so much and satisfy you?

So that's how it turns out.

function getParameters(obj) {//Get address bar parameter does not include hash value
            var search = decodeURI(location.search).replace('?', '');//Prevent Chinese miscoding
            if(typeof obj=="string"){
                var reg = new RegExp("(^|&)"+ obj +"=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if(r!=null)return  unescape(r[2]); return null;
            }else if(typeof obj=="undefined"){
                var searray = search.split("&");
                var obj = {}
                searray.map(function (item) {
                    var sarry = item.split('=');
                    obj[sarry[0]] = sarry[1];
                })
                return obj;
            }else if(obj.constructor ==Array){
                var searray = search.split("&");
                var fromarray = {}
                searray.map(function (item) {                 
                    var sarry = item.split('=');
                     if(obj.indexOf(sarry[0])>-1){
                          fromarray[sarry[0]] = sarry[1];   
                     }

                })
                return fromarray;
            }

}

// getParameters();
//  getParameters('tn');
 //getParameters(['tn','wd']);

Kiss you.

Posted by atlanta on Thu, 31 Oct 2019 13:20:49 -0700