Archive

Archive for the ‘JavaScript’ Category

Get Querystring Using Javascript

March 20, 2012 Leave a comment

Getting querystring by using a server side programming language is something very easy and straight forward most of the times, but how about getting querystring using javascript?

Below I will show you an easy function you can add it to your page to get the querystring needed from the url

<script type=”text/javascript”>
function getQuerystring(key, default_) {
if (default_ == null) default_ = “”;
key = key.replace(/[\[]/, “\\\[“).replace(/[\]]/, “\\\]”);
var regex = new RegExp(“[\\?&]” + key + “=([^&#]*)”);
var qs = regex.exec(window.location.href);
if (qs == null)
return default_;
else
return qs[1];
}
var merchant_value = getQuerystring(‘merchant’);

// to test if working

alert(merchant_value);
</script>

P.S. if you need to use this function to show/hide a control in your page add the script before the body tag (</body>) so that all the controls are loaded.

Advertisements