Use javascript to replace the parameter values in a URL. Example code _ javascript skills

Source: Internet
Author: User
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

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.