将URL中的参数提取出来作为对象

	(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; i<len; i++) {
					var urlItem = urlArray[i];
					var item = urlItem.split("=");
					urlObject[item[0]] = item[1];
				}
				return urlObject;
			}
		};
		var testUrl = "http://www.taobao.com/index.php?key0=0&key1=1&key2=2";
		var result = urlToObject(testUrl);
		for (var key in result) {
			alert(key + "=" + result[key]);
		}
	})();
posted @ 2011-08-15 20:28  张臣  阅读(460)  评论(0编辑  收藏  举报