js acquisition parameters in the URL

js acquisition parameters in the URL

the first method:

var urlParams = new URLSearchParams('?post=1234&action=edit');
console.log(urlParams.get('action')); // "edit"
View Code

 

 The second method:

function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}
View Code

url example:

http://www.runoob.com/index.php?id=1&image=awesome.jpg

Call  getQueryVariable ( "id")  returns 1.

Call  getQueryVariable ( "image")  returns "awesome.jpg".

The third method:

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

  Call the method: 
the let Parameter 1 = GetQueryString ( "parameter name 1"));

The fourth method:

function getQueryVariable(variable){
       let query = window.location.search.substring(1);
       let vars = query.split("&");
       for (let i=0;i<vars.length;i++) {
               let pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}
View Code

 

 

  • Call the method:
  • let Parameter 1 = getQueryVariable ( "parameter name 1");

 

Guess you like

Origin www.cnblogs.com/mahmud/p/11530574.html