Sep 15 2009

How To: Retrieve Querystring Paramater Values Using Javascript

Category: amilsark @ 00:03

Javascript does not have a built in way to get querystring values easily in name, value pairs like ASP.Net.

 

So how do i get the “id” value in the URL  www.myurl.com/default.aspx?id=12345  ??

 

Here’s a javascript function which I found on multiple sites that works well.

   1: function GetQuerystringParam(name, url) {
   2:     name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
   3:     var regexS = "[\\?&]" + name + "=([^&#]*)";
   4:     var regex = new RegExp(regexS);
   5:     if (typeof (url) == 'undefined') url = window.location.href;
   6:     var results = regex.exec(url);
   7:     if (results == null)
   8:         return "";
   9:     else
  10:         return results[1];
  11: }

 

If you want to just use the browser’s current URL as the target querystring, see an example below.

   1: var value = GetQuerystringParam("id");    

 

Enjoy!

Tags: , , ,

Nov 26 2008

Now using BlogEngine.Net

Category: Andy @ 20:08
This is a cool open source ASP.Net blog engine.  I bought this domain and had to get something up here ..

Tags: , ,