Retrieving GET Parameters


var ls = location.search.substring(1); var namevalue = ls.split("&"); 

Usually, GET information is evaluated on the server side, but JavaScript does also have access to this information via its location.search property. However, the data there is in name-value pairs. The following code decodes this data by using the JavaScript split() method. The resulting associative array is then shown, just to prove that it works as expected; see Figure 2.1 for the output.

Parsing the Query String (querystring.html)

<script language="JavaScript"   type="text/javascript"> var getdata = []; if (location.search.length > 1) {   var ls = location.search.substring(1);   var namevalue = ls.split("&");   for (var i=0; i<namevalue.length; i++) {     var data = namevalue[i].split("=");     getdata[data[0]] = data[1];   } } var s = ""; for (var el in getdata) {   s += el + ": " + getdata… + "\n"; } alert(s); </script> 

Figure 2.1. The data from the query string is parsed and shown.





JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net