JavaScript URL variables

  • Post author:
  • Post category:Uncategorized

Today I needed to get the value of a URL variable in JavaScript. Unlike ColdFusion, in JavaScript there isn’t a built-in scope that contains all of your URL variables. You have to manually parse the URL and split the name value pairs. Here is a function I use for this purpose:function getURLVar(urlVarName) { //divide the URL in half at the '?' var urlHalves = String(document.location).split('?'); var urlVarValue = ''; if(urlHalves[1]){ //load all the name/value pairs into an array var urlVars = urlHalves[1].split('&'); //loop over the list, and find the specified url variable for(i=0; i<=(urlVars.length); i++){ if(urlVars[i]){ //load the name/value pair into an array var urlVarPair = urlVars[i].split('='); if (urlVarPair[0] && urlVarPair[0] == urlVarName) { //I found a variable that matches, load it's value into the return variable urlVarValue = urlVarPair[1]; } } } } return urlVarValue; }So, in ColdFusion I would do this to get a URL variable:and now with this JavaScript function I can do this:var lastTabVisted = getURLVar('lastTab');