Get URL GET values using javascript

I had a situation of getting URL GET values to my web page without using server side scripting language like PHP, JSP or ASP.

So this is a simple way to get URL GET value to your web page using javaScript. When you don’t have any facilities to code server side scripts, you can use this technique to make your website more dynamic.

Simple example :

<html>
  <head>
     <script>// Import GET Vars
        (function(){
           document.$_GET = [];
           var getVarsString = String(document.location).split('?');
           if(getVarsString[1]){
              var getVars = getVarsString[1].split('&');
              for(var i=0; i<=(getVars.length); i++){
                 if(getVars[i]){
                    var getVarPair = getVars[i].split('=');
                    document.$_GET[getVarPair[0]] = getVarPair[1];
                 }
              }
           }
         })();
         function updateView(){
            document.getElementById("view").innerHTML=document.$_GET['i'];
         }
     </script>
  </head>
  <body>
      <input id="txt" type="button" value="click" onclick="updateView();" />
      <div id="view"></div>
  </body>
</html>
%d bloggers like this: