1
2
3
4http://www.adr.gov.cn/haha/haha.asp?a=1232&b=kaka%fsd&pid2=iloveyou&bb=ssdf
5var url2 = "http://www.test.com/test.php";
6
7<script language="JavaScript">
8// 设定 url 中的 QueryString 值
9function setUrlParam(url, param, v)
10{
11 var re = new RegExp("(\\\?|&)" + param + "=([^&]+)(&|$)", "i");
12 var m = url.match(re);
13 if (m)
14 {
15 return (url.replace(re, function($0, $1, $2) { return ($0.replace($2, v)); } ));
16 }
17 else
18 {
19 if (url.indexOf('?') == -1)
20 return (url + '?' + param + '=' + v);
21 else
22 return (url + '&' + param + '=' + v);
23 }
24}
25
26function getUrlParam(url, param)
27{
28 var re = new RegExp("(\\\?|&)" + param + "=([^&]+)(&|$)", "i");
29 var m = url.match(re);
30 if (m)
31 return m[2];
32 else
33 return '';
34}
35
36// test
37var url = "http://www.adr.gov.cn/haha/haha.asp?a=1232&b=kaka%fsd&pid2=iloveyou&bb=ssdf";
38
39alert(getUrlParam(url, 'a'));
40alert(getUrlParam(url, 'B'));
41alert(getUrlParam(url, 'pid2'));
42alert(getUrlParam(url, 'bb'));
43alert(getUrlParam(url, 'chen'));
44var url2 = "http://www.test.com/test.php";
45alert(setUrlParam(url, 'a', '23'));
46alert(setUrlParam(url, 'b', 'hahahaha'));
47alert(setUrlParam(url, 'bb', '我爱你'));
48alert(setUrlParam(url2, '不存在', '哈哈哈哈'));
49</script>
50
2
3
4http://www.adr.gov.cn/haha/haha.asp?a=1232&b=kaka%fsd&pid2=iloveyou&bb=ssdf
5var url2 = "http://www.test.com/test.php";
6
7<script language="JavaScript">
8// 设定 url 中的 QueryString 值
9function setUrlParam(url, param, v)
10{
11 var re = new RegExp("(\\\?|&)" + param + "=([^&]+)(&|$)", "i");
12 var m = url.match(re);
13 if (m)
14 {
15 return (url.replace(re, function($0, $1, $2) { return ($0.replace($2, v)); } ));
16 }
17 else
18 {
19 if (url.indexOf('?') == -1)
20 return (url + '?' + param + '=' + v);
21 else
22 return (url + '&' + param + '=' + v);
23 }
24}
25
26function getUrlParam(url, param)
27{
28 var re = new RegExp("(\\\?|&)" + param + "=([^&]+)(&|$)", "i");
29 var m = url.match(re);
30 if (m)
31 return m[2];
32 else
33 return '';
34}
35
36// test
37var url = "http://www.adr.gov.cn/haha/haha.asp?a=1232&b=kaka%fsd&pid2=iloveyou&bb=ssdf";
38
39alert(getUrlParam(url, 'a'));
40alert(getUrlParam(url, 'B'));
41alert(getUrlParam(url, 'pid2'));
42alert(getUrlParam(url, 'bb'));
43alert(getUrlParam(url, 'chen'));
44var url2 = "http://www.test.com/test.php";
45alert(setUrlParam(url, 'a', '23'));
46alert(setUrlParam(url, 'b', 'hahahaha'));
47alert(setUrlParam(url, 'bb', '我爱你'));
48alert(setUrlParam(url2, '不存在', '哈哈哈哈'));
49</script>
50