setting global variables in javascript from html.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sabbbih
    New Member
    • Aug 2008
    • 2

    setting global variables in javascript from html.

    hello all,
    please forgive me if this is stupid question as my knowledge of programming is limited and thanks in advance for the answers.
    i have an external javascript file test.js and it has

    test.js
    -------------------------
    [CODE=javascript]var x = null;
    var y = null;
    function goSubmit()
    {
    x = document.getEle mentById("machi ne").value;
    alert(x+"inside ");
    }

    function calling()
    {
    y = x;
    alert(y+"outsid e");
    ....somecode
    }[/CODE]

    and i have html page test.html with following code
    [HTML]<html>
    <head>
    <script language="javas cript" src="test.js"></script>
    </head>
    <body>
    <i><input type="text" onclick="this.v alue = ''" id="machine" value="Enter Node Name!" size="36" name="machine"> </i>
    <a onclick="javasc ript:goSubmit() " href="other.htm l" target="_self"> Connect</a>
    </body>
    </html>[/HTML]

    and the result i get when i input the name "system1" i got the following reults
    system1 inside
    null outside.

    so the question is there any work around possible to assign value to global variable so that they can be used later on in the script. some coding example (if possible making the above script complete) will be highly appreciated.
    Last edited by gits; Aug 31 '08, 10:09 AM. Reason: added code tags
  • Ferris
    New Member
    • Oct 2007
    • 101

    #2
    Did you call the function "calling" in Other.html ? If you want to pass variable "x" to Other.html , you should use URL parameters. test.js will be initialized when Other.html is loaded , and then variable x will be null.

    Comment

    • RamananKalirajan
      Contributor
      • Mar 2008
      • 608

      #3
      Hi, for your problem each and every page have their own instances. You can not refer the same variable in two pages. Thats why you are getting null in the "other.html ". there must be some other way to do as per your requirement. I dont know that (i.e. using global variable). but u can use form variables and through URL. it will be use to retrieve the value from one page to other. here goes an example for that

      Test.html
      [HTML]
      <html>
      <form method="get" action="others. html">
      System Name: <input type="text" name="node" id="node">
      <br/>
      <input type="submit" value="submit">
      </form>
      </html>[/HTML]

      Others.html
      [HTML]<html>
      <script language="JavaS cript">

      function doThat()
      {
      var val = location.search ;
      var x = new Array();
      x = val.split("=");
      alert(x[1]);
      }

      </script>
      <body onload="doThat( );">
      </body>
      </html>[/HTML]

      Hope this may be helpful for u.

      Regards
      Ramanan Kalirajan

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Another alternative is if you use window.opener to open a pop-up window, you can use window.opener.x to refer to the variable in the parent window.

        Comment

        Working...