Variable scope - 2

Discussion in 'Javascript' started by John Wood, Oct 22, 2003.

  1. John Wood

    John Wood Guest

    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
     
    John Wood, Oct 22, 2003
    #1
    1. Advertising

  2. John Wood <> writes:

    > Can anybody explain why my variable imonline is still 1 even though my
    > onError function has fired


    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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Oct 22, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    975
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    682
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    505
  4. David Filmer
    Replies:
    19
    Views:
    257
    Kevin Collins
    May 21, 2004
  5. Andrew Falanga
    Replies:
    2
    Views:
    205
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page