Home > Web Front-end > JS Tutorial > Two function codes for processing URLs with JavaScript_javascript skills

Two function codes for processing URLs with JavaScript_javascript skills

WBOY
Release: 2016-05-16 19:10:22
Original
1026 people have browsed it

function request(paras){ //Get the parameter value of url, case-insensitive, if there is no such parameter, return an empty string.
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 parameters must be in the form For 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 When there is only one parameter, it is a simple redirect. When there are two or more parameters, the destination URL can be dynamically specified. This function can be used for page turning functions, such as redirect("http://www.xxx.com/ list.asp?page=1","page=" parseInt(request("page")) 1), it can also be used for url type search, such as: redirect("http://www.xxx.com/search. asp","range=" escape($("range").value),"keyword=" escape($("keyword").value)), the operation of url becomes simple.
The core of redirect is to establish 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 into the destination url.

As soon as I finished posting the log, I thought I could improve it a bit and add a parameter to decide whether to open the destination URL in a new window.


/*
The first parameter is the current url, such as http://localhost/demo.asp?xxx=zzz,
The second and subsequent parameters must be in the form 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;
}
}



Based on my thoughts before leaving get off work yesterday, I will modify it again and put the second parameter at the end.

/*
Use Age:
redirect(url,[paras_1],[paras_2],...,[paras_n],[newWin])
paras_n: url parameter, form Such as page=1 or type=news etc.
newWin: The last parameter of the function, Boolean type. When it is true, use a new window (window.open) to open the url, otherwise use the current window (location.open) to open it. The default value is false.

Example:
redirect("http://www.google.com/search","q=hello","start=20",true); //Will search for "hello" on google ", and turn to page 3 and open in a new window.
redirect("http://www.xxx.com/listpage.asp","page=" parseInt(request("page")) 1); //"Next page" in the page turning 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;
}
}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template