Why is imageNormal undefined?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Anderson

    Why is imageNormal undefined?

    This is driving me crazy - I'm following through some basic rollover code
    (below with all extra content stuff stripped away. When I mouseover the
    image, IE errors, reporting "imagesHili te is undefined". Why?

    I've used the rem'ed out alert to prove the preLoad code runs and the mouse
    events pass the expected 'type' argument. Anyone show what I'm doing wrong?

    ######### code starts #########
    <html><head><ti tle>Image Swap Test</title>
    <script language="JavaS cript" type="text/JavaScript">
    function preloadImages() {
    if (document.image s) {
    var imagesNormal = new Object();
    imagesNormal["home"] = new Image(130, 20);
    imagesNormal["home"].src = 'images/homenav1.gif';
    var imagesHilite = new Object();
    imagesHilite["home"] = new Image(130, 20);
    imagesHilite["home"].src = 'images/homenav2.gif';
    //alert(imagesNor mal["home"].src + ' | ' +
    imagesHilite["home"].src);
    }
    }
    function setImage(type) {
    if (document.image s) {
    if (type == 'hilite') {
    document.images['home'].src = imagesHilite['home'].src;
    //alert('hilite') ;
    return true;
    } else if (type == 'normal') {
    document.images['home'].src = imagesNormal['home'].src;
    //alert('normal') ;
    return true;
    }
    }
    return false;
    }
    </script></head>
    <body bgcolor="#FFFFF F" text="#000000" onLoad="preload Images();"
    leftmargin="0" topmargin="0">
    <a href="home.htm" onmouseover="re turn setImage('hilit e');"
    onmouseout="ret urn setImage('norma l');"><img name="home" height="20"
    width="130" border="0" src="images/homenav1.gif" alt="Home Page"></a>
    </body>
    </html>
    #### code ends ###########
    Thanks,

    Mark


  • Dan Brussee

    #2
    Re: Why is imageNormal undefined?

    In article <be286b$7en$1$8 [email protected] mon.co.uk>,
    mark@notmeyeard ley.demon.co.uk says...[color=blue]
    > This is driving me crazy - I'm following through some basic rollover code
    > (below with all extra content stuff stripped away. When I mouseover the
    > image, IE errors, reporting "imagesHili te is undefined". Why?
    >
    > I've used the rem'ed out alert to prove the preLoad code runs and the mouse
    > events pass the expected 'type' argument. Anyone show what I'm doing wrong?
    >
    > ######### code starts #########
    > <html><head><ti tle>Image Swap Test</title>
    > <script language="JavaS cript" type="text/JavaScript">
    > function preloadImages() {
    > if (document.image s) {
    > var imagesNormal = new Object();
    > imagesNormal["home"] = new Image(130, 20);
    > imagesNormal["home"].src = 'images/homenav1.gif';
    > var imagesHilite = new Object();
    > imagesHilite["home"] = new Image(130, 20);
    > imagesHilite["home"].src = 'images/homenav2.gif';
    > //alert(imagesNor mal["home"].src + ' | ' +
    > imagesHilite["home"].src);
    > }
    > }
    > function setImage(type) {
    > if (document.image s) {
    > if (type == 'hilite') {
    > document.images['home'].src = imagesHilite['home'].src;
    > //alert('hilite') ;
    > return true;
    > } else if (type == 'normal') {
    > document.images['home'].src = imagesNormal['home'].src;
    > //alert('normal') ;
    > return true;
    > }
    > }
    > return false;
    > }
    > </script></head>
    > <body bgcolor="#FFFFF F" text="#000000" onLoad="preload Images();"
    > leftmargin="0" topmargin="0">
    > <a href="home.htm" onmouseover="re turn setImage('hilit e');"
    > onmouseout="ret urn setImage('norma l');"><img name="home" height="20"
    > width="130" border="0" src="images/homenav1.gif" alt="Home Page"></a>
    > </body>
    > </html>
    > #### code ends ###########
    > Thanks,
    >
    > Mark
    >
    >[/color]

    You need to make the preloaded images global. Right now they loose scope
    outside your preloadImages function. Simply moving the two

    var imagesNormal = new Object();
    var imagesHilite = new Object();

    outside ALL functions and removing them from inside the preloadImages
    function "should" take care of it for you.



    --

    Remove NOT from email address to reply. AntiSpam in action.

    Comment

    Working...