JS Get and Modify URL Parameters
# JS Get and Modify URL Parameters
# Get URL Parameters
/**
* Get a parameter from the URL
* @param arg Parameter name
* @returns
*/
function getURLString(arg) {
var reg = new RegExp("(^|&)" + arg + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null)
return unescape(r[2]);
return null;
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# Modify URL Parameters
/**
* Modify a URL parameter
* @param url The URL to modify
* @param arg The parameter name to modify
* @param arg_val The new value
* @returns {String}
*/
function changeURLArg(url, arg, arg_val) {
var pattern = arg + '=([^&]*)';
var replaceText = arg + '=' + arg_val;
if (url.match(pattern)) {
var tmp = '/(' + arg + '=)([^&]*)/gi';
tmp = url.replace(eval(tmp), replaceText);
return tmp;
} else {
if (url.match('[\?]')) {
return url + '&' + replaceText;
} else {
return url + '?' + replaceText;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Modify URL Parameters Without Refreshing the Page
https://www.cnblogs.com/wuting/p/8946927.html (opens new window)
# URL Encoding and Decoding
For example, on a system using UTF-8, in the URL http://www.example.com/q=hello, non-ASCII characters are not valid URL characters, so the browser automatically encodes them. For instance, the Chinese word for "Spring Festival" would be converted to http://www.example.com/q=%E6%98%A5%E8%8A%82. The first character becomes %E6%98%A5, and the second character becomes %E8%8A%82. This is because their UTF-8 encodings are E6 98 A5 and E8 8A 82 respectively, and each byte is prefixed with a percent sign to form the URL encoding.
JavaScript provides four methods for URL encoding/decoding:
encodeURI()encodeURIComponent()decodeURI()decodeURIComponent()
Edit (opens new window)
Last Updated: 2026/03/21, 12:14:36