-2

I'm aware of window.location.host and window.location.pathname, but is there anyway to pull the extra parameters of a link like this?

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

I would like to end up with a dynamic variable that is equal to "u123" but without the double quotes.

Thanks!

  • do you really want a variable of "u123" or a variable called "u" with a value of "123"? – scunliffe Feb 28 '13 at 23:17
  • Shmiddty yeah I took a look at that answer, but I didn't want to use a regex formula. Wesley I didn't want to use jquery because I am setting it up for a client who doesn't have jquery enabled. What I was looking for was window.location.search. Thanks guys! – Victor Torres Mar 01 '13 at 00:35

3 Answers3

0

window.location! MDN window.location

martriay
  • 5,632
  • 3
  • 29
  • 39
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);
matthewk
  • 1,841
  • 17
  • 31
0

For a basic example of getting all the page parameters into a usable variable try this:

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;
  }
}

thus in your case pageParams['u'] = "123"

scunliffe
  • 62,582
  • 25
  • 126
  • 161