Tuesday 22 May 2012

JQuery : Extracting URL parameters using regular expression


____________________________________________________________

A normal URL contains a number of parameters.


for example: http://books.google.com.pk/bkshp?hl=en&tab=wp


this link contains a parameter ( &tab=wp)


Extract all parameters from a URL, the following code may help you.




$.urlParam = function(name){
   var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
   if(results != null)
   return results[1] || 0;


   
}
var bookTab = $.urlParam(' tab ');


where  bookTab  is javascript variable which store the value of url parameter named "tab".










No comments:

Post a Comment