1,通过js如何获取get 请求参数呢?
- /***
- * get request query string
- * @returns {{}}
- */
- var getQueryParams = function () {
- var i, ilen, strs, keyName, keyValue,
- params = {},
- path = window.location.pathname,
- url = window.location.href;
- if (url.indexOf("?") > -1) {
- var index = url.indexOf("?");
- strs = url.substring(index + 1);
- strs = strs.split("&");
- ilen = strs.length;
- for (i = 0; i < ilen; i++) {
- var indexEqual = strs[i].indexOf('=');
- if (indexEqual == -1) {
- keyName = strs[i];
- keyValue = '';
- } else {
- keyName = strs[i].substring(0, indexEqual);
- keyValue = strs[i].substring(indexEqual + 1) || "";
- }
- if (keyName == "callback") keyValue = decodeURIComponent(keyValue);
- params[keyName] = keyValue;
- }
- }
- return params;
- };
测试:http://localhost:63342/js_svn/request_queryString.html?username=whuang&age=32&callback=?a=b&bb&1=2
打印:
Object {1: "2", username: "whuang", age: "32", callback: "?a=b", bb: ""}
参考:http://hw1287789687.iteye.com/blog/2234658