Saturday, February 23, 2013

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://phattrienweb.com/?technology=jquery&blog=jquerybyexample". 
 
//
var tech = GetQueryStringParams('technology');
var blog = GetQueryStringParams('blog');
//
 
OR....
 
 // Read a page's GET URL variables and return them as an associative array.
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;
}
 


 var first = getUrlVars()["qid"];


http://phattrienweb.com/

0 Comments:

Post a Comment