Showing posts with label Web Development. Show all posts
Showing posts with label Web Development. Show all posts

Thursday, November 25, 2010

How to get the querystring value in JavaScript

Unlike most server side programming languages, JavaScript doesn't provide a standard way of parsing these values. I have tried a couple of different methods of doing it including regular expressions; however, I decided to opt out of regular expressions because I could never get it the way I wanted it.

So after a while I came out with this function:

  var DeveloperCaster = {
            QueryString:function(key) {
                var address = window.location.href;
                var args = address.split("?");
                if (args.length == 2) {
                    var arg;
                    if (args[1].indexOf("&") != -1) {
                        arg = args[1].split("&");
                        if (arg.length > 0) {
                            for (var i = 0; i < arg.length; i++) {
                                var sArg = arg[i].split("=");
                                if (sArg.length > 0) {
                                    if ((sArg[0] == key)) {
                                        return sArg[1];
                                    }
                                }
                            }
                        }
                    } else {
                        arg = args[1].split("=");
                        if (arg.length > 0) {
                            if ((arg[0] == key)) {
                                return arg[1];
                            }
                        }
                    }
                }
                return "";
            }
        };

How to use it:

<script language="JavaScript">
   if (DeveloperCaster.QueryString("Param").length > 0) {
            alert(DeveloperCaster.QueryString("Param"));
   } 
</script>

Tuesday, November 23, 2010

Free JavaScript Compression Tools Online

Here is  list of cool free JavaScript compression tools available online.
  1. YUI Compressor - Yahoo's offering is widely regarded as the top compression tool.  YUI Compressor is written in Java (requires Java >= 1.4) and relies on Rhino to tokenize the source JavaScript file. 
  2. YUI .NET ports - A C# port of Yahoo's tool; really good option if you are a C# programmer.
  3. JSCOMPRESSOR - A handy web front end to the YUI.NET project.