Home >Web Front-end >JS Tutorial >Extract the parameters in the URL as the object implementation code in js_javascript skills

Extract the parameters in the URL as the object implementation code in js_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:03:24985browse
复制代码 代码如下:

(function() {
var urlToObject = function(url) {
var urlObject = {};
if (/?/.test(url)) {
var urlString = url.substring(url.indexOf("?") 1);
var urlArray = urlString.split("&");
for (var i=0, len=urlArray.length; ivar urlItem = urlArray[i];
var item = urlItem.split("=");
urlObject[item[0]] = item[1];
}
return urlObject;
}
};
var testUrl = "http://tools.jb51.net/index.php?key0=0&key1=1&key2=2";
var result = urlToObject(testUrl);
for (var key in result) {
alert(key "=" result[key]);
}
})();
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn