Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 18:03:24
Original
929 people have browsed it
复制代码 代码如下:

(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]);
}
})();
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!