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>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: