Connecting Tech Pros Worldwide Help | Site Map

Variable scope - 2

  #1  
Old July 20th, 2005, 12:18 PM
John Wood
Guest
 
Posts: n/a
Can anybody explain why my variable imonline is still 1 even though my
onError function has fired , value = 0 when printed by Javascript
document.write at end. I thought that
VAR imonline = 1
should have given me a global variable
TIA


<HTML>

<SCRIPT LANGUAGE="JavaScript">
var imonline = 1;
function imageError(theImage) {imonline=0; document.JWonline.src =
"2_yellow.jpg";}
</script>
</HEAD>

<BODY BGCOLOR="#b5ccb8">
<H2><CENTER>Is the green button loaded 2</CENTER></H2>
<!-- 2_greenx.jpg does not exist for this test -->
<img name="JWonline" src="2_greenx.jpg" align=left border=0
onError="imageError(this)"
onAbort="imageError(this)">

<p>Yellow button indicates offline<br>
Green button indicates online

<!-- since yellow button is on, so imonline should be 0 -->
<SCRIPT LANGUAGE="JavaScript">
document.write("<BR>online=",imonline);
</script>

</BODY>
</HTML>

John Wood
  #2  
Old July 20th, 2005, 12:18 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a

re: Variable scope - 2


John Wood <john@lortim.demon.co.uk> writes:
[color=blue]
> Can anybody explain why my variable imonline is still 1 even though my
> onError function has fired[/color]

No it hasn't. When your document.write is executed, the image is still
being loaded (or waiting to fail loading). It will probably not finish
loading/failing the image before the page has been fully processed.

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable Scope in JavaScript pbmods insights 1 July 26th, 2007 06:52 PM
Variable Scope in VBA for MS Access msquared insights 0 June 5th, 2007 10:55 AM
variable scope! Jeff answers 5 November 26th, 2006 11:05 PM
Getting value from Tkinter entry widget: variable scope difficulty Kevin Walzer answers 2 November 20th, 2006 06:15 AM
variable scope Michael G answers 7 August 16th, 2005 02:05 PM