-2

我知道window.location.host 和window.location.pathname,但是有没有像这样提取链接的额外参数?

http://www.example.com/test?u=123

我想得到一个等于“u123”但没有双引号的动态变量。

谢谢!

4

3 回答 3

0

window.locationMDN window.location

于 2013-02-28T23:15:44.253 回答
0
var oGetVars = {};

if (window.location.search.length > 1) {
  for (var aItKey, nKeyId = 0, aCouples = window.location.search.substr(1).split("&"); nKeyId < aCouples.length; nKeyId++) {
    aItKey = aCouples[nKeyId].split("=");
    oGetVars[unescape(aItKey[0])] = aItKey.length > 1 ? unescape(aItKey[1]) : "";
  }
}

// alert(oGetVars.yourVar);
于 2013-02-28T23:19:30.427 回答
0

有关将所有页面参数放入可用变量的基本示例,请尝试以下操作:

var pageParams = {};
if(location.search != ''){
  var searchStr = location.search;
  searchStr = searchStr.substr(1);
  var searchStrArr = searchStr.split('&');
  var pageParamPair, pageParamKey, pageParamValue;
  for(var i=0;i<searchStrArr.length;i++){
    pageParamPair = searchStrArr[i].split('=');
    pageParamKey = pageParamPair[0];
    pageParamValue = pageParamPair[1];
    pageParams[pageParamKey] = pageParamValue;
  }
}

因此在你的情况下pageParams['u'] = "123"

于 2013-02-28T23:20:51.850 回答