0 Replies Latest reply on Dec 9, 2009 6:09 PM by nealc991

    HOWTO: Pass Variables Between HTML Pages

    nealc991

      Passing variables from one page to another is a common practice in various server side languages such as PHP, JSP, ASP etc.

       

      But when it comes to passing vars from one page to the next on HTML pages the most common way is via FORMS.

       

      There is another way through HTML & Javascript to pass variables and I find it quiet usefull when developing applications with AIR.

       

      Below is an example URL with vars:

       

      http://example.com/page1.htm?name=My Name&addr=111 Second Street&city=Somewhere&st=MO

       

      Now inside page1.htm we put the following function:

       

      function get_vars(){

          // take query string from URL and return VARS

      var vars = new Array();

          var query = window.location.search.substring(1);

          var parms = query.split('&');

          for (var i = 0; i < parms.length; i++) {

              var pos = parms[i].indexOf('=');

              if (pos > 0) {

                  var key = parms[i].substring(0, pos);

                  var val = parms[i].substring(pos + 1);

                  vars[key] = val;

              }else{

              return 'No Variables Found!';

              }

          }

          return vars;

      }

       

      By issuing a command such as:

       

      var variables = get_vars();

       

      "variables" will be loaded with the results passed to the current page from the previous one.

       

      The results would be that "variables" would contain the following:

       

      variables['name']  = "My Name"

      variables['addr"]  = "111 Second Street"

      variables['city']  = "Somewhere"

      variables['st']  = "MO"

       

      Hope this technique has been a usefull contribution to the AIR development community (I know I use it a lot).