Preload images not working

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

    #1

    Preload images not working

    I'm preloading images with the standard code:

    default1 = new Image(); default1.src = "images/image1.gif";
    changed1 = new Image(); changed1.src = "images/image1-on.gif";
    default2 = new Image(); default2.src = "images/image2.gif";
    changed2 = new Image(); changed2.src = "images/image2-on.gif";

    And this is my rollover code:

    var iName="";
    function Ichange(p) {
    var pSrc=eval(p+ ".src");
    document[iName].src = pSrc;
    }

    But for some reason on the first mouseover it doesn't seem like the
    image has been preloaded. There is a delay, like it's looking for the
    rollover image, which should have already been preloaded. Anyone have
    any ideas why?

    Here is the inline code for a mouseover:
    <a href="test.html " onMouseOver="iN ame='image1'; Ichange('change d1')"
    onMouseOut="Ich ange('default1' )"><img src="images/image1.gif"
    width="122" height="19" border="0" name="image1"></a>

    Thanks for any help you can provide!
  • Evertjan.

    #2
    Re: Preload images not working

    hoolie wrote on 27 sep 2004 in comp.lang.javas cript:
    [color=blue]
    > I'm preloading images with the standard code:
    >
    > default1 = new Image(); default1.src = "images/image1.gif";
    > changed1 = new Image(); changed1.src = "images/image1-on.gif";
    > default2 = new Image(); default2.src = "images/image2.gif";
    > changed2 = new Image(); changed2.src = "images/image2-on.gif";
    >
    > And this is my rollover code:
    >
    > var iName="";
    > function Ichange(p) {
    > var pSrc=eval(p+ ".src");
    > document[iName].src = pSrc;
    >}
    >
    > But for some reason on the first mouseover it doesn't seem like the
    > image has been preloaded. There is a delay, like it's looking for the
    > rollover image, which should have already been preloaded. Anyone have
    > any ideas why?
    >
    > Here is the inline code for a mouseover:
    > <a href="test.html " onMouseOver="iN ame='image1'; Ichange('change d1')"
    > onMouseOut="Ich ange('default1' )"><img src="images/image1.gif"
    > width="122" height="19" border="0" name="image1"></a>[/color]

    eval() is evil and not needed here.
    var pSrc=p+ ".src"; works the same, as you need a string, but ...

    use the onmouseover of <img>, not <a>,
    and you do not need the function overhead.

    =============== ===========

    try this [in the <head> section!!]:

    default1 = new Image(); default1.src = "images/image1.gif";
    changed1 = new Image(); changed1.src = "images/image1-on.gif";
    default2 = new Image(); default2.src = "images/image2.gif";
    changed2 = new Image(); changed2.src = "images/image2-on.gif";

    and this in the <body>

    <a href="test.html ">
    <img src="images/image1.gif"
    onMouseOver="th is.src='changed 1.src'"
    onMouseOut="thi s.src='default1 .src'"
    width="122" height="19" border="0">
    </a>

    not tested

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress,
    but let us keep the discussions in the newsgroup)

    Comment

    • Michael Winter

      #3
      Re: Preload images not working

      On 27 Sep 2004 07:33:50 GMT, Evertjan. <exjxw.hannivoo rt@interxnl.net >
      wrote:
      [color=blue]
      > hoolie wrote on 27 sep 2004 in comp.lang.javas cript:[/color]

      [snip]
      [color=blue][color=green]
      >> And this is my rollover code:
      >>
      >> var iName="";[/color][/color]

      As Evertjan suggested, placing the rollover code on the image itself means
      you don't have to worry about its name. You can use the 'this' operator to
      refer to the image object.
      [color=blue][color=green]
      >> function Ichange(p) {
      >> var pSrc=eval(p+ ".src");[/color][/color]

      The eval function is almost never necessary. It's faster to use bracket
      accessors:

      var pSrc = window[p].src;

      Global variables are properties of the global object. In a browser
      environment, this is the window object. The bracket property accessors
      take the enclosed expression, p in this case, convert it to a string
      (which it already is), and finds the property with that name.
      [color=blue][color=green]
      >> document[iName].src = pSrc;[/color][/color]

      It's better to use the images collection:

      document.images[iName].src = pSrc;

      but as I said, if Ichange was activated from the image, not the link, that
      could be simplified to:

      this.src = pSrc;

      and the iName variable wouldn't be necessary.
      [color=blue][color=green]
      >> }
      >>
      >> But for some reason on the first mouseover it doesn't seem like the
      >> image has been preloaded. There is a delay, like it's looking for the
      >> rollover image, which should have already been preloaded. Anyone have
      >> any ideas why?[/color][/color]

      It could be your security settings. My rollover code seemed to act
      strangely with IE. When I set my website as a Trusted Site, there was no
      problem. I haven't worked out which setting is causing the problem, though.

      Take a look at:

      <URL:http://www.mlwinter.pw p.blueyonder.co .uk/image-swap/>

      If the rollover image there (the text, 'On') doesn't appear when activated
      (remember to give it time to preload!), it's a setting in your browser.
      You might also want to think about downloading and using that code. It's a
      lot easier to maintain, in my opinion, than writing a huge list of
      filenames. Moreover, as you're using a naming system close to what the
      code requires only a few changes to your filenames will be necessary to
      work with your site.

      [snip]
      [color=blue]
      > <a href="test.html ">
      > <img src="images/image1.gif"
      > onMouseOver="th is.src='changed 1.src'"
      > onMouseOut="thi s.src='default1 .src'"[/color]

      That won't work. You're assigning a simple string, "changed1.s rc", as the
      source URL. As I posted above, you need:

      this.src = window['changed1'].src
      [color=blue]
      > width="122" height="19" border="0">
      > </a>
      >
      > not tested[/color]

      Perhaps you should have. :P :)

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • Evertjan.

        #4
        Re: Preload images not working

        Michael Winter wrote on 27 sep 2004 in comp.lang.javas cript:
        [color=blue][color=green]
        >> onMouseOut="thi s.src='default1 .src'"[/color]
        >
        > That won't work. You're assigning a simple string, "changed1.s rc", as
        > the source URL. As I posted above, you need:
        >
        > this.src = window['changed1'].src
        >[color=green]
        >> width="122" height="19" border="0">
        >> </a>
        >>
        >> not tested[/color]
        >
        > Perhaps you should have. :P :)
        >[/color]

        I see what you mean.

        I always [till now] used an array:

        this.src = imgArray[n].src

        but this
        [color=blue]
        > this.src = window['changed1'].src[/color]

        should not be necessary
        as this should work [unquoted]:

        this.src=change d1.src

        ??


        --
        Evertjan.
        The Netherlands.
        (Please change the x'es to dots in my emailaddress,
        but let us keep the discussions in the newsgroup)

        Comment

        • Michael Winter

          #5
          Re: Preload images not working

          On 27 Sep 2004 10:36:51 GMT, Evertjan. <exjxw.hannivoo rt@interxnl.net >
          wrote:
          [color=blue]
          > Michael Winter wrote on 27 sep 2004 in comp.lang.javas cript:[/color]

          [snip]
          [color=blue][color=green]
          >> this.src = window['changed1'].src[/color]
          >
          > should not be necessary
          > as this should work [unquoted]:
          >
          > this.src=change d1.src[/color]

          Yes, that will do fine. I was still thinking about the previous comments
          to the OP where a variable look-up was necessary.

          Mike

          --
          Michael Winter
          Replace ".invalid" with ".uk" to reply by e-mail.

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: Preload images not working

            hoolie wrote:
            [color=blue]
            > I'm preloading images with the standard code:
            >
            > default1 = new Image(); default1.src = "images/image1.gif";
            > changed1 = new Image(); changed1.src = "images/image1-on.gif";
            > default2 = new Image(); default2.src = "images/image2.gif";
            > changed2 = new Image(); changed2.src = "images/image2-on.gif";[/color]

            There is nothing standard about this code, the ECMAScript compliant
            syntax aside. You are using host objects like Image without testing
            prior, and you are assuming that the object returned has a "src"
            property you can add/overwrite.
            [color=blue]
            > And this is my rollover code:
            >
            > var iName="";
            > function Ichange(p) {
            > var pSrc=eval(p+ ".src");[/color]

            The use of eval(...) is neither required nor recommended here, see the FAQ.
            [color=blue]
            > document[iName].src = pSrc;[/color]

            One should use the document.images collection, and one should not depend on
            that named/ID'd ("img") elements become properties of the document object
            as it is in the IE DOM.
            [color=blue]
            > }
            >
            > But for some reason on the first mouseover it doesn't seem like the
            > image has been preloaded. There is a delay, like it's looking for the
            > rollover image, which should have already been preloaded. Anyone have
            > any ideas why?
            > [...][/color]

            Your code is garbage. Try this instead, for it has been peer-reviewed
            and tested intensively:

            <http://www.pointedears .de/scripts/test/hoverMe>

            And keep in mind that so-called preloading can do harm as it only works
            reasonably if the client's/proxy's cache settings are appropriate.


            PointedEars

            Comment

            Working...