Get Querystring with JavaScript - Parse/Get QueryString with Client-Side JavaScript
Nearly all server-side programming languages have built-in functions to retrieve querystring values of a URL. In web browsers you can access the querystring with client-side JavaScript, but there is no standard way to parse out the name/value pairs. So here is a function to return a parameter you specify. The following javascript code snippet facilitates Javascript's built in regular expressions to retrieve value of the key. Optionally, you can specify a default value to return when key does not ...
exist.
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];
The getQuerystring function is simple to use. Let's say you have the following URL:
http://www.bloggingdeveloper.com?author=bloggingdeveloper
and you want to get the "author" querystring's value:
var author_value = getQuerystring('author');
If you execute code shown in the above line, the author_value will be bloggingdeveloper. The query string is parsed by the regular expression and the value of the author_value parameter is retrieved. The function is smart in a couple of ways. For example, if you have an anchor in your URL like our example URL above does (#top) the getQuerystring() function knows to stop before the # character. Also, if a requested parameter doesn't exist in the query string and the default value is not specified as a function parameter then an empty string is returned instead of a null. Want automatic updates? Subscribe to our RSS feed[6] or
Compress Javascript with compressjavascript.com - Free Online Javascript Compression ToolInternet Applications with Web 2.0 features make heavy use of JavaScript. As rich web applications are being built with larger JavaScript, the need for JavaScript compression to keep bandwidth and page load times as small as possible by decreasing the size of the files served is becoming more import...[29]JavaScript substring vs. substr with Examples - The Difference Between JavaScript String Extraction FunctionsWhen you write JavaScript, you need to know string manipulation functions. There is a slight difference between JavaScript substring() and JavaScript substr() which are both used to extract characters from strings.[30]JavaScript setTimeout Function - JavaScript Timing EventsJavaScript features a couple of methods that lets you run a piece of JavaScript code (javascript function) at some point in the future. These methods are: setTimeout() and setInterval() In this tutorial, I'll explain how setTimetout() method works, and give a real world example.[31] SponsoredTweets referral badge[32]Free CMS[33]
Hide
- 232 comments on this story
PRO