Js gets or sets a small example of the current window url parameter
- 2020-03-26 21:25:24
- OfStack
//Gets 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<query.length;i++){
var kv = query[i].split('=');
if(kv[0] == param){
return kv[1];
}
}
return null;
}
//Sets the value of the 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{
if(query == ''){
location.search = '?'+param+'='+value;
}else{
location.search = '?'+query+'&'+param+'='+value;
}
}
}
Notice location.search gets the url, right? Content between start and # (including? But not #).
In the previous page-turning code, which used the above two functions
//The previous page
function page_pre(current_page,page_total){
if(current_page <= 1 || current_page > page_total){
return false;
}
var pre_page = parseInt(current_page) - 1;
set_param('page',pre_page);
}
//The 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);
}