Home >Web Front-end >JS Tutorial >How to query string parameters in javascript_javascript skills

How to query string parameters in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:17:401027browse

The example in this article describes the method of querying string parameters in javascript. Share it with everyone for your reference. The specific implementation method is as follows:

Copy code The code is as follows:
/* Parse the query string and return an object containing all parameters*/

function getQueryStringArgs(){

//Get the query string and remove the question mark at the beginning
var qs = (location.search.length > 0 ? location.search.substring(1) : '');

//Object to save data
args = {};

//Get each item
var items = qs.length ? qs.split('&') : [],
item = null,
name = null,
//Use in for loop
i = 0, len = items.length;

//Add each item to the args object one by one
for(i = 0; i < len; i ){
Item = items[i].split('=');
name = decodeURIComponent(item[0]);
Value = decodeURIComponent(item[1]);

If(name.length){
             args[name] = value;                                         }  
}  
Return args;
}
In this way, you can easily get the corresponding parameter values ​​in the URL.

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn