Using JavaScript to process two function codes of a URL _ javascript skills

Source: Internet
Author: User
Use JavaScript to process the two function codes of the URL: function request (paras) {// obtain the url parameter value, case-insensitive. If this parameter is not found, an empty string is returned.
Var url = location. href;
Var paraString = url. substring (url. indexOf ("? ") + 1, url. length). split ("&");
Var paraObj = {}
For (I = 0; j = paraString [I]; I ++ ){
ParaObj [j. substring (0, j. indexOf ("= ")). toLowerCase ()] = j. substring (j. indexOf ("=") + 1, j. length );
}
Var returnValue = paraObj [paras. toLowerCase ()];

If (typeof (returnValue) = "undefined "){
Return "";
} Else {
Return returnValue;
}
}
Function redirect () {// The first parameter is the current url, such as http: // localhost/demo. asp? Xxx = zzz. The second and later parameter formats must be xxx = yyy, mm = bbbbb. The final jump url is http: // localhost/demo. asp? Xxx = yyy & aaa = bbb
If (arguments. length = 1 ){
Location. href = arguments [0];
Return;
} Else {
Var paraObj = {};
If (arguments [0]. indexOf ("? ")! =-1 ){
Var curUrlParas = arguments [0]. substring (arguments [0]. indexOf ("? ") + 1, arguments [0]. length). split ("&");
For (I = 0; j = curUrlParas [I]; I ++ ){
ParaObj [j. substring (0, j. indexOf ("= ")). toLowerCase ()] = j. substring (j. indexOf ("=") + 1, j. length );
}
}
For (I = 1; j = arguments [I]; I ++ ){
ParaObj [j. substring (0, j. indexOf ("= ")). toLowerCase ()] = j. substring (j. indexOf ("=") + 1, j. length );
}
Var newURL = "";
For (key in paraObj ){
NewURL + = key + "=" + paraObj [key] + "&";
}
If (arguments [0]. indexOf ("? ")! =-1 ){
NewURL = arguments [0]. substring (0, arguments [0]. indexOf ("? ") + 1) + newURL. substring (0, newURL. length-1 );
} Else {
NewURL = arguments [0] + "? "+ NewURL. substring (0, newURL. length-1 );
}
Location. href = newURL;
Return;
}
}


If the second function redirect has only one parameter, it is a simple redirection. When there are two or more parameters, the target url can be dynamically specified. This function can be used for paging, for example, redirect ("http://www.xxx.com/list.asp? Page = 1 "," page = "+ parseInt (request (" page ") + 1), can also be used for url-type searches such as redirect (" http://www.xxx.com/search.asp ", "range =" + escape ($ ("range "). value), "keyword =" + escape ($ ("keyword "). value), the url operation becomes simple.
The core of redirect is to create a url parameter table (hash table). The second and later parameters of the function are added to the hash table, and the table is serialized as the destination url.

After sending the log, you can improve it by adding a parameter to determine whether to open the target url in a new window.


/*
The first parameter is the current url, such as http: // localhost/demo. asp? Xxx = zzz,
The second and later parameter format must be xxx = yyy, mm = bbbbb
The final jump url is http: // localhost/demo. asp? Xxx = yyy & aaa = bbb
*/
Function redirect (){
If (arguments. length = 0 ){
Return;
}
If (arguments. length = 1 ){
Location. href = arguments [0];
Return;
} Else if (arguments. length = 2 ){
(Arguments [1] = true )? Window. open (arguments [0]): location. href = arguments [0];
Return;
} Else {
Var paraObj = {};
If (arguments [0]. indexOf ("? ")! =-1 ){
Var curUrlParas = arguments [0]. substring (arguments [0]. indexOf ("? ") + 1, arguments [0]. length). split ("&");
For (I = 0; j = curUrlParas [I]; I ++ ){
ParaObj [j. substring (0, j. indexOf ("= ")). toLowerCase ()] = j. substring (j. indexOf ("=") + 1, j. length );
}
}
For (I = 2; j = arguments [I]; I ++ ){
ParaObj [j. substring (0, j. indexOf ("= ")). toLowerCase ()] = j. substring (j. indexOf ("=") + 1, j. length );
}
Var newURL = "";
For (key in paraObj ){
NewURL + = key + "=" + paraObj [key] + "&";
}
If (arguments [0]. indexOf ("? ")! =-1 ){
NewURL = arguments [0]. substring (0, arguments [0]. indexOf ("? ") + 1) + newURL. substring (0, newURL. length-1 );
} Else {
NewURL = arguments [0] + "? "+ NewURL. substring (0, newURL. length-1 );
}
Arguments [1] = true? Window. open (newURL): location. href = newURL;
Return;
}
}



According to the idea before leaving work yesterday, modify the second parameter and put it at the end.

/*
Use Age:
Redirect (url, [paras_1], [paras_2],..., [paras_n], [newWin])
Paras_n: url parameter in the form of page = 1 or type = news.
NewWin: The last parameter of the function, Boolean. If it is set to true, open the url in a new window (window. open). Otherwise, open the url in the current window (location. open. The default value is false.

Example:
Redirect ("http://www.google.com/search", "q = hello", "start = 20", true); // search for "hello" on google and go to page 3rd, open it in a new window.
Redirect ("http://www.xxx.com/listpage.asp", "page =" + parseInt (request ("page") + 1); // the next page in the paging function ".

*/
Function redirect (){
If (arguments. length = 0 ){
Return;
}
If (arguments. length = 1 ){
Location. href = arguments [0];
Return;
} Else if (arguments. length = 2 & typeof (arguments [1]) = "boolean "){
(Arguments [1] = true )? Window. open (arguments [0]): location. href = arguments [0];
Return;
} Else {
Var paraObj = {};
If (arguments [0]. indexOf ("? ")! =-1 ){
Var curUrlParas = arguments [0]. substring (arguments [0]. indexOf ("? ") + 1, arguments [0]. length). split ("&");
For (I = 0; j = curUrlParas [I]; I ++ ){
ParaObj [j. substring (0, j. indexOf ("= ")). toLowerCase ()] = j. substring (j. indexOf ("=") + 1, j. length );
}
}
Var j = arguments. length;
For (I = 1; I if (typeof (arguments [I]) = "boolean "){
Break;
}
ParaObj [arguments [I]. substring (0, arguments [I]. indexOf ("= ")). toLowerCase ()] = arguments [I]. substring (arguments [I]. indexOf ("=") + 1, arguments [I]. length );
}
Var newURL = "";
For (key in paraObj ){
NewURL + = key + "=" + paraObj [key] + "&";
}
If (arguments [0]. indexOf ("? ")! =-1 ){
NewURL = arguments [0]. substring (0, arguments [0]. indexOf ("? ") + 1) + newURL. substring (0, newURL. length-1 );
} Else {
NewURL = arguments [0] + "? "+ NewURL. substring (0, newURL. length-1 );
}
If (typeof (arguments [length-1]) = "boolean" & arguments [length-1] = true ){
Window. open (newURL );
} Else {
Location. href = newURL;
}
Return;
}
}

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.