User Id :    Password :      New Member   Forgot Password
 
Get query string value using JavaScript
Description This is simple codesnippet to get query string value in JS.   No. of Views     9042
  Rating     0
Author Ferhan Siddiqui   Posted On     15 Aug 2011
Tags ASP.NET,Java Script    

Sample Code   Download Code

 

Get query string value using JavaScript


Here is the JavaScript function getQuerystring which finds the key form query string and returns the value.

 

/*
* <summary>
* Get the querystring value
* </summary>
* <param name="key">A string contains the querystring key</param>
* <param name="defaultVal">Object which get returns when there is not key</param>
**/

function getQuerystring(key, defaultVal) {
    if (defaultVal == null) {
        defaultVal = "";
    }
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null) {
        return defaultVal;
    }
    else {
        return qs[1];
    }
}

 

About Author

About Author I m a software developer having more than 3.5+ years experience. Ferhan
No Photo
 
Country N/A
Company Saffron Infotech Private Limited
Home Page N/A

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

     
Write your comment here.  
Comment
Verification Code   
  
    
 
Section sponsored by