Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am currently setting over a hundred global variables by performing individual checks against the URL string.

var Query_MenuTiles = getParameterByName("menutiles")
var Query_Link = getParameterByName("link")
var Query_Speed = getParameterByName("speed")

Unless it would slow things down, I assume that setting keys and properties of an object would be a cleaner way of doing this.

The getParameterByName function is as follows:

function getParameterByName(name, source ) { 
    if (typeof source == 'undefined') {
        source = window.location.search
    }
    if ( user_fastquery == '1' ) {
        return search[name] || ''
    }
    else {
      name = name.toLowerCase()
      name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
      var regexS = "[\\?&]" + name + "=([^&#]*)"; //"=([^&#]*)"; 
      var regex = new RegExp(regexS);
      var query_x = 'back'
      var query_y = 'frame'
      if ( name != query_x ) {
        if ( source.indexOf("&" + query_x + "=") >= 0 ) {
            var array_source = source.split('&' + query_x + '=')
            source = array_source[0]
            var source_tail = array_source[1]
        }
        else if ( source.indexOf("?" + query_x + "=") >= 0 ) {
            var array_source = source.split("?" + query_x + "=" )
            source = array_source[0]
        }       
      }
      var results = regex.exec( source );
      if(results == null)
        return "";
      else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
    }
}
share|improve this question

1 Answer 1

up vote 1 down vote accepted

You are correct that it would be much faster to parse all the parameters once into an object and then just access them from there rather than refind every single parameter from scratch each time:

function getParameters(sourceURL) {
    var pairs, parts, i, output = {};
    sourceURL = sourceURL || window.location.search;
    // strip off leading ?
    if (sourceURL.charAt(0) === "?") {
        sourceURL = sourceURL.slice(1);
    }
    // split into individual pairs
    pairs = sourceURL.split("&");
    for (i = 0; i < pairs.length; i++) {
        // split each pair
        parts = pairs[i].split("=");
        if (parts.length < 2) {
            parts.push("");
        }
        // make key always be lowercase and store key/value pair
        output[decodeURIComponent(parts[0]).toLowerCase()] = decodeURIComponent(parts[1]);
    }
    return output;
}

var params = getParameters();

Then, you can fetch an individual parameter anytime from the params object. In fact, you won't even need to store them all in globals since there already stored in this one variable.

var Query_MenuTiles = params["menutiles"];

Working demo: http://jsfiddle.net/jfriend00/pj5ocazd/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.