Two function codes to handle URLs with JavaScript _javascript tips

Source: Internet
Author: User
function request (paras) {//Gets the parameter value of the URL, case-insensitive, and returns an empty string, without this argument.
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 subsequent parameter forms 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
}
}


The second function redirect if there is only one argument, is a simple redirect, when there are 2 or more parameters, you can dynamically specify the destination URL, the function can be used for paging function, such as 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 operation of the URL becomes simpler.
The core of redirect is to create a URL parameter table (hash table), the second and subsequent parameters of the function are added to the hash table, and finally the table is serialized as the destination URL.

Just send out the log, think can be improved, add a parameter, decide whether to open the destination URL with a new window.


/*
The first parameter is the current URL, such as http://localhost/demo.asp?xxx=zzz,
The second and subsequent parameter forms must be xxx=yyy, MM=BBBBB
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 work yesterday, revise it, put the second parameter to the last side.

/*
Use Age:
Redirect (Url,[paras_1],[paras_2],..., [Paras_n],[newwin])
Paras_n:url parameter, form such as page=1 or type=news.
Newwin: The last argument of the function, the Boolean type, when True, opens the URL with a new window (window.open) or opens with the current window (Location.open). The default value is False.

Example:
Redirect ("Http://www.google.com/search", "Q=hello", "start=20", true); Google will search for "Hello" and turn to page 3rd and open with a new window.
Redirect ("http://www.xxx.com/listpage.asp", "page=" +parseint (Request ("page")) +1); Next page in the paging feature.

*/
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.