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

Source: Internet
Author: User

Copy codeThe Code is as follows:
// Obtain the param parameter value 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;
}

// Set the param value 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;
}
}
}

Note: What is the url obtained by location. search? Content (including? But does not contain #).

In the previous Code, two functions are used.

Copy codeThe Code is as follows:
// 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 );
}

// 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 );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.