SearchSearch   

Problem with global variables and buttons

 
   Webmaster Forums (Home) -> Javascript RSS
Next:  Is it not well to extend Object.prototype derictl..  
Author Message
viola823

External


Since: Aug 12, 2007
Posts: 4



(Msg. 1) Posted: Sun Aug 12, 2007 7:39 am
Post subject: Problem with global variables and buttons
Archived from groups: comp>lang>javascript (more info?)

Hi
I have a simple code where I try to change a global variable by the
function run by a button with 'onClick' event, but my solution won't
work... here's the code:

---------------------------
<html>
<head>
<script language="JavaScript">
var v = 0;
function change_v() {
v++;
}
function show_v() {
alert(v);
}
</script>
</head>
<body>
<form method="post">
<input type="button" name="b1" value="change_v()"
onClick="change_v();">
<input type="button" name="b2" value="show_v()" onClick="show_v();">
</form>

<script language="JavaScript">
document.write('v = ' + v);
</script>
</body>
</html>
---------------------------

Here:

<input type="button" name="b1" value="change_v()"
onClick="change_v();">
<input type="button" name="b2" value="show_v()" onClick="show_v();">

variable 'v' is changing and displaing correctly by the functions ,
but here:

document.write('v = ' + v);

'v' all the time equals 0...

What's wrong? Any ideas?
Thanks a lot for any suggestions!

Greets
Ps. sorry for my english:)
Back to top
viola823

External


Since: Aug 12, 2007
Posts: 4



(Msg. 2) Posted: Sun Aug 12, 2007 7:53 am
Post subject: Re: Problem with global variables and buttons
Archived from groups: per prev. post (more info?)

....And i don't want to just display this variable like here:
http://groups.google.pl/group/comp.lang.javascript/browse_thread/threa...c2ee79f
but do on it some actions (like 'if' by example)...
Back to top
viola823

External


Since: Aug 12, 2007
Posts: 4



(Msg. 3) Posted: Sun Aug 12, 2007 8:16 am
Post subject: Re: Problem with global variables and buttons
Archived from groups: per prev. post (more info?)

> Nothing. It would appear that you think setting an event handler attribute
> (here: onClick) makes its value execute automatically when the corresponding
> element is rendered; that is not the case. The event handler code is
> executed when the event occurs, not before. In this case, it is executed
> when you click the button (hence "onclick").

So there's no any way to do it by the buttons? :/

> You should also validate your markup:http://vaidator.w3.org/

This code was written only for example, I wouldn't place it
elsewhere:)
Back to top
Lee

External


Since: Jun 09, 2004
Posts: 1556



(Msg. 4) Posted: Sun Aug 12, 2007 8:42 am
Post subject: Re: Problem with global variables and buttons
Archived from groups: per prev. post (more info?)

viola823 DeleteThis @o2.pl said:
>
>> Nothing. It would appear that you think setting an event handler attribute
>> (here: onClick) makes its value execute automatically when the corresponding
>> element is rendered; that is not the case. The event handler code is
>> executed when the event occurs, not before. In this case, it is executed
>> when you click the button (hence "onclick").
>
>So there's no any way to do it by the buttons? :/

Thomas has misunderstood your problem.
It's annoying to have to pull him out of the killfile again
to see how he's confused somebody else.

When you use document.write() to display the value of a variable
on a page, it writes the current value as of the time that
document.write() statement is executed. If you later change the
value by executing an event handler, the string that has already
been written to the page will not change.

Your functions work.


--
Back to top
viola823

External


Since: Aug 12, 2007
Posts: 4



(Msg. 5) Posted: Sun Aug 12, 2007 10:13 am
Post subject: Re: Problem with global variables and buttons
Archived from groups: per prev. post (more info?)

Thanks both of you, i'll try those solutions, now i have some new
ideas:)
Back to top
Thomas 'PointedEars' Lahn

External


Since: Sep 05, 2004
Posts: 3405



(Msg. 6) Posted: Sun Aug 12, 2007 11:48 am
Post subject: Re: Problem with global variables and buttons
Archived from groups: per prev. post (more info?)

viola823.RemoveThis@o2.pl wrote:
> <script language="JavaScript">
^^^^^^^^^^^^^^^^^^^^^^
> var v = 0;
^^^^^^^^^^
> function change_v() {
> v++;
> }
> function show_v() {
> alert(v);
> }
> </script>
> [...]
> </head>
> <body>
> [...]
> <form method="post">
^^^^^^^^^^^^^^
method="post" is unnecessary if you don't submit anything. For that matter,
the `form' element is unnecessary as well.

> <script language="JavaScript">
^^^^^^^^^^^^^^^^^^^^^^
> document.write('v = ' + v);
> </script>
> [...]
> Here:
>
> <input type="button" name="b1" value="change_v()"
> onClick="change_v();">
> <input type="button" name="b2" value="show_v()" onClick="show_v();">
>
> variable 'v' is changing and displaing correctly by the functions ,
> but here:
>
> document.write('v = ' + v);
>
> 'v' all the time equals 0...
>
> What's wrong?

Nothing. It would appear that you think setting an event handler attribute
(here: onClick) makes its value execute automatically when the corresponding
element is rendered; that is not the case. The event handler code is
executed when the event occurs, not before. In this case, it is executed
when you click the button (hence "onclick").

You should also validate your markup: http://vaidator.w3.org/


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>
Back to top
Thomas 'PointedEars' Lahn

External


Since: Sep 05, 2004
Posts: 3405



(Msg. 7) Posted: Sun Aug 12, 2007 11:48 am
Post subject: Re: Problem with global variables and buttons
Archived from groups: per prev. post (more info?)

Thomas 'PointedEars' Lahn wrote:
> You should also validate your markup: http://vaidator.w3.org/

http://validator.w3.org/
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Back to top
Thomas 'PointedEars' Lahn

External


Since: Sep 05, 2004
Posts: 3405



(Msg. 8) Posted: Sun Aug 12, 2007 5:59 pm
Post subject: Re: Problem with global variables and buttons
Archived from groups: per prev. post (more info?)

viola823.DeleteThis@o2.pl wrote:
>> Nothing. It would appear that you think setting an event handler attribute
>> (here: onClick) makes its value execute automatically when the corresponding
>> element is rendered; that is not the case. The event handler code is
>> executed when the event occurs, not before. In this case, it is executed
>> when you click the button (hence "onclick").
>
> So there's no any way to do it by the buttons? :/

Of course there is a way. It is just not the way you tried. What
document.write() writes to the document is simply static text. Even if you
write a variable value, there is no magic attached to it like a reference to
the variable. You call document.write() while the document is loading, and
the value it writes is the unchanged value of the variable.

If you want to change document content you have to use appropriate
techniques. document.write() is one of them; however, If you call
document.write() after the document has loaded, it usually overwrites the
document content, so that is probably not a viable solution here. (That
is what the VK article you referred fails to explain. Never ever rely on
what is said in one of VK's articles. His notion of Voodoo programming is
unreliable at best.)

Now there are several other techniques you may try:

- modify the `value' property of a HTMLInputElement object:

http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-6043025

Example: <input ... onclick="this.form.elements["foo"].value = v;">

- modify the standards compliant node value of a text node:

http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-F68D080

Example: document.getElementById("foo").firstChild.nodeValue = v;

- modify the proprietary `innerHTML' property of an element object

http://msdn2.microsoft.com/en-us/library/ms533897.aspx

http://developer.mozilla.org/en/docs/DOM_Client_Object_Cross-Reference:DOM_HTML

Example: document.all("foo").innerHTML = v;

All of these aproaches, which may be combined, require proper feature tests
so that they do not cause a run-time error when the required properties are
not supported:

http://www.jibbering.com/faq/faq_notes/not_browser_detect.html

>> You should also validate your markup:http://vaidator.w3.org/
>
> This code was written only for example, I wouldn't place it
> elsewhere:)

It is illogical to expect non-Valid examples to work as if they were Valid.

http://diveintomark.org/archives/2003/05/05/why_we_wont_help_you


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Back to top
Display posts from previous:   
       Webmaster Forums (Home) -> Javascript
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum