-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgetParam.js
More file actions
28 lines (23 loc) · 757 Bytes
/
Copy pathgetParam.js
File metadata and controls
28 lines (23 loc) · 757 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* @param {key}
* @param {[url]}
* @return {[value | ""]}
*/
var getParam = function(name, url) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
if (url) {
if (url.indexOf("?") != -1) {
url = url.split("?")[1];
} else if (window.location.search.length > 1) {
url = decodeURI(window.location.search.substr(1));
} else {
return "";
}
}
var r = url.match(reg);
if (r != null) return decodeURI(r[2]);
return "";
}
var xml = "http://www.baidu.com/system/index.html?name=\"夏明\"&age=9";
// var string ="name=\"小明\"&age=19";
console.log(getParam("name", xml));