By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,199 Members | 1,551 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,199 IT Pros & Developers. It's quick & easy.

Question about global variables in javascript

100+
P: 130
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

Expand|Select|Wrap|Line Numbers
  1. ////global vars//////
  2. var txbox1=20;
  3. var txbox2=30;
  4. var border=1;
  5.  
  6. function show_var()
  7. {
  8. alert("this is txbo1: " + txbox1 + " this is txbox2: " + txbox2 + " this is size border: "+border);
  9. }
from some reson it all comes out as undifand.
the function show_var is called onload from body
thanks agian
its great forum
Oct 29 '07 #1
Share this Question
Share on Google+
6 Replies


gits
Expert Mod 5K+
P: 5,352
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
Oct 29 '07 #2

100+
P: 130
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. //GLOBAL VARIBLES
  3. var size_x_banner=120;
  4. var size_y_banner=600;
  5. var border_size1=1;
  6. var color_font="black";    
  7. var bgcolor_r="white";
  8. function gen_code()            
  9. {    
  10.  
  11.     document.getElementsByTagName("pre")[0].innerHTML ="";        //clear previues displays in the <pre>
  12.     var body = document.getElementsByTagName("pre")[0];         //put the table in <pre> here </pre>
  13.     var tbl  = document.createElement("table");                 //create table element
  14.     var tblBody = document.createElement("tbody");                 
  15.  
  16.         for (var j = 0; j < 4; j++)                     //creating all cells
  17.         { 
  18.             var row = document.createElement("tr");               //creates a table row
  19.             row.setAttribute("height","39");                    //BTW cant control the higeht in  html?
  20.             for (var i = 0; i < 1; i++) 
  21.             {
  22.                 var cell = document.createElement("td");                        //Create a <td> element
  23.                 var linktd='<a href="www.google.com">google</a>';
  24.                 cell.innerHTML = linktd 
  25.                 cell.setAttribute("align","center");                            //set the aligen for the tdl    
  26.                 row.appendChild(cell);                                             //put the <td> at the end of the table row
  27.             }
  28.             tblBody.appendChild(row);                                            //add the row to the end of the table body
  29.         }
  30.     tblBody.appendChild(row);
  31.     tbl.appendChild(tblBody);
  32.  
  33.         tbl.appendChild(tblBody);                                                //put the <tbody> in the <table>
  34.         body.appendChild(tbl);                                                    // appends <table> into <body>
  35.         tbl.setAttribute("cellpadding","0");                                    // no cellpadding in the table
  36.         tbl.setAttribute("cellspacing","0");                                    // no cell spacing in the table
  37.         tbl.style.color = color_font;                                            //font color on the links
  38.         tbl.style.backgroundColor = bgcolor_r;                                    //bgcolor for table
  39.         tbl.setAttribute("height",size_y_banner);
  40.         tbl.setAttribute("width",size_x_banner);
  41.         tbl.setAttribute("border",border_size1);            
  42. }
  43. </script>
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?
Oct 29 '07 #3

gits
Expert Mod 5K+
P: 5,352
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_var();">
</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
Oct 29 '07 #4

100+
P: 130
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_var();">
</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
Oct 29 '07 #5

gits
Expert Mod 5K+
P: 5,352
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
Oct 29 '07 #6

100+
P: 130
Expand|Select|Wrap|Line Numbers
  1. //GLOBAL VARIBLES
  2. size_x_banner=120;
  3. size_y_banner=600;
  4. border_size1=1;
  5. color_font="black";    
  6. bgcolor_r="white";
  7. function gen_code()            
  8. {the function above }
i remove the "var" and its working perfect
Oct 29 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.