1

I came across old web application which has the code to read the parameters from the URL of the site. The code is full of string processing.

http://hostname.domain[port number]/application name?arg1=value1...&argN=valueN 

Considering that URL parameters are always encoded it is litter difficult to rely on string processing. Also not sure if one can rely 100% on the URLEncode/Decode functions of the JavaScript.

function getURLParameters(){
if (location.search != "")
{
    var x = location.search.substr(1).split(";")
    for (var i=0; i<x.length; i++)
    {
        var y = x[i].split("=");
        alert("Key '" + y[0] + "' has the content '" + y[1]+"'")
    }
}   
}

Now that made me think if there is any better way we can read values from URL ? OR should we go ahead and change the approach itself by sending values using POST/dumping JSON object on cliente ? Please help me with this.

Anil Namde
  • 6,452
  • 11
  • 63
  • 100
  • 1
    This has been answered here [get query string parameters](http://stackoverflow.com/questions/901115/get-querystring-values-with-jquery) – DhruvPathak Feb 10 '11 at 06:28

1 Answers1

2

I found this little query string plugin that seems to do the job

The example:

http://terenz.io/?test=yes&javascript&something=1

$.getQueryParam("test") returns "yes"
$.getQueryParam("javascript") returns ""
$.getQueryParam("something") returns "1"
$.getQueryParam("somethingelse") returns undefined
TJB
  • 13,367
  • 4
  • 34
  • 46