How to get variables from a URL using Javascript

How to get variables from a URL using javascript.

// ex url: http://www.example.com/index.html?hello=bonjour&goodevening=bonsoir
var hash = getUrlVars();
alert(hash['hello']); // prints 'bonjour'
function getUrlVars()
{
    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;
}
This entry was posted in Javascript and tagged , , . Bookmark the permalink.

2 Responses to How to get variables from a URL using Javascript

  1. mari_ku says:

    it was very interesting to read snpts.com
    I want to quote your post in my blog. It can?
    And you et an account on Twitter?

  2. emt training says:

    Great site. A lot of useful information here. I’m sending it to some friends!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>