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: javascipt, querystring, asp.net, web development
f2ee1b7a-626d-4593-96c5-9c9aacf0c77f|0|.0