Home >Web Front-end >JS Tutorial >A small example of js getting or setting the url parameters of the current window_javascript skills

A small example of js getting or setting the url parameters of the current window_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:20:111285browse

Copy code The code is as follows:

// Get the value of the param parameter in the current window url
function get_param(param){
var query = location.search.substring(1).split('&');
for(var i=0;i var kv = query[i].split('=');
if(kv[0] == param){
return kv[1];
}
}
return null;
}

//Set the value of param in the current window url
function set_param(param,value){
var query = location.search.substring(1);
var p = new RegExp("( ^|&" param ")=[^&]*");
if(p.test(query)){
query = query.replace(p,"$1=" value);
location.search = '?' query;
}else{
}else{
             location.search = '?' query '&' param '=' value;
                                                                                         ? Content between the beginning and # (including? but not #).

In the previous page turning code, the above two functions were used



Copy code

The code is as follows:

var pre_page = parseInt(current_page) - 1;
set_param( 'page',pre_page);
}

//Next page
function page_next(current_page,page_total){
if(current_page < 1 || current_page >= page_total){
return false;
}
var next_page = parseInt(current_page) 1; set_param('page',next_page);

}