Javascript copies PHP $_GET to get the parameters in the URL

  • 2020-03-30 02:58:48
  • OfStack



function getArgs() {
    var args = {};
    var query = location.search.substring(1); // Get query string
    var pairs = query.split("&");
                   // Break at ampersand
     for(var i = 0; i < pairs.length; i++) {
            var pos = pairs[i].indexOf('=');
             // Look for "name=value"
            if (pos == -1) continue;  // If not found, skip
               var argname = pairs[i].substring(0,pos); // Extract the name
               var value = pairs[i].substring(pos+1); // Extract the value
               value = decodeURIComponent(value); // Decode it, if needed
               args[argname] = value;  // Store as a property
        }
    return args; // Return the object           
} 

/* URL :  http://www.baidu.com?user=funsion&age=26 */
alert( getArgs()['user'] );  //Output funsion
alert( getArgs()['age'] );  //The output of 26


Related articles: