This article mainly introduces the sample code for replacing the parameter values in the URL with javascript. If you need it, you can refer to it, I hope it will be helpful to everyone. Today I met a need to use javascript to replace some parameters in the url. I think of a parseUrl function I just found online, which can be implemented as follows:
The Code is as follows:
// Analyze the url
Function parseURL (url ){
Var a = document. createElement ('A ');
A. href = url;
Return {
Source: url,
Protocol: a. protocol. replace (':',''),
Host: a. hostname,
Port: a. port,
Query: a. search,
Params: (function (){
Var ret = {},
Seg = a. search. replace (/^ \? /, ''). Split ('&'),
Len = seg. length, I = 0, s;
For (; I <len; I ++ ){
If (! Seg [I]) {continue ;}
S = seg [I]. split ('= ');
Ret [s [0] = s [1];
}
Return ret;
})(),
File: (a. pathname. match (// ([^ \/? #] +) $/I) | [, '']) [1],
Hash: a. hash. replace ('#',''),
Path: a. pathname. replace (/^ ([^ \/])/, '/$1 '),
Relative: (a. href. match (/tps? : \/[^ \/] + (. +)/) | [, '']) [1],
Segments: a. pathname. replace (/^ \ //, ''). split ('/')
};
}
// Replace the parameter value of the same name in myUrl
Function replaceUrlParams (myUrl, newParams ){
/*
For (var x in myUrl. params ){
For (var y in newParams ){
If (x. toLowerCase () = y. toLowerCase ()){
MyUrl. params [x] = newParams [y];
}
}
}
*/
For (var x in newParams ){
Var hasInMyUrlParams = false;
For (var y in myUrl. params ){
If (x. toLowerCase () = y. toLowerCase ()){
MyUrl. params [y] = newParams [x];
HasInMyUrlParams = true;
Break;
}
}
// If there are no parameters, append them.
If (! HasInMyUrlParams ){
MyUrl. params [x] = newParams [x];
}
}
Var _ result = myUrl. protocol + ": //" + myUrl. host + ":" + myUrl. port + myUrl. path + "? ";
For (var p in myUrl. params ){
_ Result + = (p + "=" + myUrl. params [p] + "&");
}
If (_ result. substr (_ result. length-1) = "&"){
_ Result = _ result. substr (0, _ result. length-1 );
}
If (myUrl. hash! = ""){
_ Result + = "#" + myUrl. hash;
}
Return _ result;
}
// Auxiliary output
Function w (str ){
Document. write (str +"
");
}
Var myURL = parseURL ('HTTP: // abc.com: 8080/dir/index.html? Id = 255 & m = hello # top ');
W ("myUrl. file =" + myURL. file) // = 'index.html'
W ("myUrl. hash =" + myURL. hash) // = 'top'
W ("myUrl. host =" + myURL. host) // = 'abc. com'
W ("myUrl. query =" + myURL. query) // = '? Id = 255 & m = hello'
W ("myUrl. params =" + myURL. params) // = Object = {id: 255, m: hello}
W ("myUrl. path =" + myURL. path) // = '/dir/index.html'
W ("myUrl. segments =" + myURL. segments) // = Array = ['dir', 'index.html ']
W ("myUrl. port =" + myURL. port) // = '000000'
W ("myUrl. protocol =" + myURL. protocol) // = 'http'
W ("myUrl. source =" + myURL. source) // = 'HTTP: // abc.com: 8080/dir/index.html? Id = 255 & m = hello # top'
Var _ newUrl = replaceUrlParams (myURL, {id: 101, m: "World", page: 1, "page": 2 });
W ("
The new url is :")
W (_ newUrl); // http://abc.com: 8080/dir/index.html? Id = 101 & m = World & page = 2 # top