473,396 Members | 1,933 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,396 software developers and data experts.

Global variable does not want to change...

Hello, I have a global variable which does not want to change :
<header>
....
<script type="text/javascript" language="JavaScript">
var i=1;
function swap()
{
var window.i = 1 - window.i ;
}
.....
</script>
....
<body>

<A href="" ... onclick="swap()"<img src="pix/star.gif" </A>

when clicking on 'star.gif', i does not change from 1 to 0 ...

Any clue ?
Thanks,
Jimmy

Nov 8 '06 #1
4 2303

"ji***********@gmail.com дµÀ£º
"
Hello, I have a global variable which does not want to change :
<header>
...
<script type="text/javascript" language="JavaScript">
var i=1;
function swap()
{
var window.i = 1 - window.i ;
~~~you reclaim the value, delete the "var" will be ok
}
....
</script>
...
<body>

<A href="" ... onclick="swap()"<img src="pix/star.gif" </A>

when clicking on 'star.gif', i does not change from 1 to 0 ...

Any clue ?
Thanks,
Jimmy
Nov 8 '06 #2
Using global variable is not a good choose. Remenber global function is
always blind to global namespace. The best choose is, use "this.i"
instead of "window.i".

Nov 8 '06 #3
Lee
Lich_Ray said:
>
Using global variable is not a good choose. Remenber global function is
always blind to global namespace. The best choose is, use "this.i"
instead of "window.i".
If I understand you correctly, you're wrong.
functions have full access to global variables.
That's what "global" means:

<html>
<body>
<script type="text/javascript">
var globalVariable="Hello, world!";
function globalFunction() {
alert(globalVariable);
globalVariable="Goodbye";
}
globalFunction();
alert(globalVariable);
</script>
</body>
</html>
--

Nov 8 '06 #4
ji***********@gmail.com wrote:

[snip]
<script type="text/javascript" language="JavaScript">
The language attribute is deprecated and redundant. Omit it.
var i=1;
function swap()
{
var window.i = 1 - window.i ;
That is a syntax error: variable declarations consist of a comma
separated list of /identifiers/, with optional initialisers. The
character sequence, "window.i" is not an identifier.

function swap() {
i = 1 - i;
}

will do. You don't want a variable declaration within the function body:
that would make i a local variable.

[snip]

Mike
Nov 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: mrbog | last post by:
I have an array/hash that stores path information for my app. As in, what directory this is in, what directory that's in, what the name of the site is, what the products are called, etc. It's...
88
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from...
10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
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...
6
by: Soha | last post by:
I'm a beginner in using javascript and i need a reply so urgent; i have a problem in using javascript with asp the problem is that i want to define a global variable and then change the value of it...
9
by: Shapper | last post by:
Hello, I am declaring a variable in my aspx.vb code as follows: Public Class catalogue Public productid As String Private Sub Page_Load ... I have an image button where I call the...
3
by: Stephen Kay | last post by:
Sorry fi this is a stupid question - I come from a c/c++ background, and the global variables in php aren't making sense to me. If I want to declare a global variable, say in a global.php file,...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.