Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Wednesday, September 10, 2014

URL params parsing on pure JavaScript

ES3 compatible URL params parsing on pure JavaScript:

/**
* Detects hash params and builds an array of values
*
* We have the following URL: "http://www.domain.com/path/page.html#param1=value1&param2=value2"
* this function will extract the part: "param1=value1&param2=value2" and build an array:
* param1 = value1
* param2 = value2
*
* inspired by: http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
*
* @returns {Array}
*/
function getHashVars()  {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('&');
    for(var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]); 
        vars[hash[0]] = hash[1];
    }
    return vars; 
}