In this post, I would like to share a little jQuery code snippet that makes getting URL parameters and their values more convenient.
Recently, while working on one of my projects, I needed to read and get parameter values from URL string of the current page that was constructed and sent by PHP script. I came across this short and sweet JavaScript code snippet by Roshambo that does just that.
// Read a page's GET URL variables and return them as an associative array. |
The function returns an array/object with your URL parameters and their values. For example, consider we have the following URL:
http://www.example.com/? me=myValue&name2=SomeOtherValue |
Calling getUrlVars() function would return you the following array:
{ |
To get a value of first parameter you would do this:
var first = getUrlVars()["me"]; |
To make the script syntax to look more jQuery like syntax I rewrote it as an extension for jQuery:
$.extend({ |
Now you can use it in the following way:
// Get object of URL parameters |
If you have better solution, just tell me !
0 comments:
Post a Comment