Question about global variables in javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Amzul
    New Member
    • Oct 2007
    • 130

    Question about global variables in javascript

    hello all agian,
    i feel like i am asking to many qustion. :\

    i have a problam and i cant think of any sulotion for it.

    in my script i declare few GLOBAL VARIBLAS but the function dont know them

    [CODE=javascript]////global vars//////
    var txbox1=20;
    var txbox2=30;
    var border=1;

    function show_var()
    {
    alert("this is txbo1: " + txbox1 + " this is txbox2: " + txbox2 + " this is size border: "+border);
    }[/CODE]
    from some reson it all comes out as undifand.
    the function show_var is called onload from body
    thanks agian
    its great forum
    Last edited by gits; Oct 29 '07, 10:20 AM. Reason: fix code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    seems to be strange ... i tested it right now ... and it works. could you post the page so that we may see the call and the declaration as you did it?

    kind regards

    Comment

    • Amzul
      New Member
      • Oct 2007
      • 130

      #3
      [CODE=javascript]<script type="text/javascript">
      //GLOBAL VARIBLES
      var size_x_banner=1 20;
      var size_y_banner=6 00;
      var border_size1=1;
      var color_font="bla ck";
      var bgcolor_r="whit e";
      function gen_code()
      {

      document.getEle mentsByTagName( "pre")[0].innerHTML =""; //clear previues displays in the <pre>
      var body = document.getEle mentsByTagName( "pre")[0]; //put the table in <pre> here </pre>
      var tbl = document.create Element("table" ); //create table element
      var tblBody = document.create Element("tbody" );

      for (var j = 0; j < 4; j++) //creating all cells
      {
      var row = document.create Element("tr"); //creates a table row
      row.setAttribut e("height","39" ); //BTW cant control the higeht in html?
      for (var i = 0; i < 1; i++)
      {
      var cell = document.create Element("td"); //Create a <td> element
      var linktd='<a href="www.googl e.com">google</a>';
      cell.innerHTML = linktd
      cell.setAttribu te("align","cen ter"); //set the aligen for the tdl
      row.appendChild (cell); //put the <td> at the end of the table row
      }
      tblBody.appendC hild(row); //add the row to the end of the table body
      }
      tblBody.appendC hild(row);
      tbl.appendChild (tblBody);

      tbl.appendChild (tblBody); //put the <tbody> in the <table>
      body.appendChil d(tbl); // appends <table> into <body>
      tbl.setAttribut e("cellpadding" ,"0"); // no cellpadding in the table
      tbl.setAttribut e("cellspacing" ,"0"); // no cell spacing in the table
      tbl.style.color = color_font; //font color on the links
      tbl.style.backg roundColor = bgcolor_r; //bgcolor for table
      tbl.setAttribut e("height",size _y_banner);
      tbl.setAttribut e("width",size_ x_banner);
      tbl.setAttribut e("border",bord er_size1);
      }
      </script>[/CODE]
      that a part of the code i cant get the VAR into the function.
      ,
      there are few function in the code that change the var (size color etc...)
      after the function change_zise()
      working i can use the size var but if i didnt start it its undefind :(
      why?

      BTW gits how did u put the javascript code?
      Last edited by gits; Oct 29 '07, 12:07 PM. Reason: fix code tags - [code=javascript]

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        hi ...

        to syntax-highlight the code simply use [code=javascript] instead of [code] ...

        have a look at the following working (simplyfied) example:

        [HTML]<script type="text/javascript">
        var txbox1=20;
        var txbox2=30;
        var border=1;

        function show_var()
        {
        alert("this is txbo1: " + txbox1 + " this is txbox2: " + txbox2 + " this is size border: "+border);
        }
        </script>

        <body onload="show_va r();">
        </body>
        [/HTML]
        this should work for you too! ... if not ... we need to know what you assign to that variables during code execution ...

        kind regards

        Comment

        • Amzul
          New Member
          • Oct 2007
          • 130

          #5
          Originally posted by gits
          hi ...

          to syntax-highlight the code simply use [code=javascript] instead of [code] ...

          have a look at the following working (simplyfied) example:

          [HTML]<script type="text/javascript">
          var txbox1=20;
          var txbox2=30;
          var border=1;

          function show_var()
          {
          alert("this is txbo1: " + txbox1 + " this is txbox2: " + txbox2 + " this is size border: "+border);
          }
          </script>

          <body onload="show_va r();">
          </body>
          [/HTML]
          this should work for you too! ... if not ... we need to know what you assign to that variables during code execution ...

          kind regards
          i discover the mistake i done, i am not sure why but
          if i declare it without the var infront its all good.
          dont know what is the reason but its true.
          maybe its becuz its .php thats the only thing i can come out
          but in anycase without the var in the front
          all the function can see the varibles like they should
          thanks for the help

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            hi ...

            so you are saying that you don't declare the variable with the var keyword? ... could you please post an example so that we may see it? i cannot think why the var keyword should make any problem ... or do is misunderstand you?

            btw. ... glad to hear you got it working ... but i'm very interested in the solution/reason for the problem ...

            kind regards

            Comment

            • Amzul
              New Member
              • Oct 2007
              • 130

              #7
              [CODE=javascript]
              //GLOBAL VARIBLES
              size_x_banner=1 20;
              size_y_banner=6 00;
              border_size1=1;
              color_font="bla ck";
              bgcolor_r="whit e";
              function gen_code()
              {the function above }[/CODE]i remove the "var" and its working perfect

              Comment

              Working...