473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Question about global variables in javascript

130 100+
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
6 1593
gits
5,390 Expert Mod 4TB
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
Amzul
130 100+
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
5,390 Expert Mod 4TB
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
Amzul
130 100+
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
5,390 Expert Mod 4TB
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
Amzul
130 100+
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

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

Similar topics

4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
5
by: Richard A. DeVenezia | last post by:
Dear Experts: Suppose I have global variables: x1, x2, x3 and I have a function that needs to assign a value to a new global variable x4 something like function foo () { count = 0;
7
by: Al | last post by:
Hi, Can I turn on/off the browser's screen updating until the entire page is loaded ? i.e. is it possible to show a page only when it is completely in browser's memory ? I don't want to show a...
22
by: Tom Moroow | last post by:
Hi, I'm pretty new to javascript and was wondering how you would piece together a variable name and then assign it a value. I want to create a hidden field and assign it a value based on the value...
6
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
6
by: 182719 | last post by:
Hi, can someone please explain variables and what I am doing wrong here? <script> x=1; alert(x); </script>
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.