Monday, June 25, 2012

How to get querystring value using jQuery

In this post, I will show you how to get the QueryString variable value using jQuery. I have created a function which returns value of any querystring variable.



//Code Starts
function GetQueryStringParams(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) 
        {
            return sParameterName[1];
        }
    }
}​
//Code Ends

And this is how you can use this function assuming the URL is,
 "http://dummy.com/?technology=jquery&blog=jquerybyexample". 

//
var tech = GetQueryStringParams('technology');
var blog = GetQueryStringParams('blog');
//

No comments: