image precaching

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

    image precaching

    Hi!

    This does not seem to work:

    function cacheimage(sour ce){
    myimg=new Image()
    myimg.src=sourc e
    }
    cacheimage('clo sed1.jpg')

    Is this because the myimg object is destroyed as soon as the function
    returns, or for some other reason? Does anyone have a simple, one argument,
    void returning function with where you can give the url of an image and have
    it precahed like this?

    Any answer must work in IE5+.

    Thanks!


  • Lasse Reichstein Nielsen

    #2
    Re: image precaching

    "viza" <none@example.i nvalid> writes:
    [color=blue]
    > This does not seem to work:[/color]

    HOW does it not work? I.e., what did you expect to happen and what really
    happens? And where do you put the code on a page?
    [color=blue]
    > function cacheimage(sour ce){
    > myimg=new Image()
    > myimg.src=sourc e
    > }
    > cacheimage('clo sed1.jpg')[/color]

    Nothing obviously wrong with the code. I would put semicolons after the
    statements for readability.
    [color=blue]
    > Is this because the myimg object is destroyed as soon as the function
    > returns,[/color]

    No, mostly because it isn't destroyed. You are assigning the new image
    to a global variable. Still, even local variables are usually not garbage
    collected soon enough to prevent the image from being fetched
    [color=blue]
    > or for some other reason?[/color]

    If it really doesn't work at all, it must be another reason.
    [color=blue]
    > Does anyone have a simple, one argument,
    > void returning function with where you can give the url of an image and have
    > it precahed like this?[/color]

    Yours look perfectly fine to me. Until we can see how it fails, it is hard
    to say anything else.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • viza

      #3
      Re: image precaching

      and then Lasse Reichstein Nielsen said:
      [color=blue]
      > "viza" <none@example.i nvalid> writes:[color=green]
      >> This does not seem to work:[/color]
      >
      > HOW does it not work? I.e., what did you expect to happen and what really
      > happens? And where do you put the code on a page?[/color]

      Try it:


      It seems to be working in Moz at home, but IE5.0 at work seems to be not
      doing it.

      Comment

      • Janwillem Borleffs

        #4
        Re: image precaching


        "viza" <none@example.i nvalid> wrote in message
        news:bgal3g$ov$ 1@titan.btinter net.com...[color=blue]
        > Hi!
        >
        > This does not seem to work:
        >
        > function cacheimage(sour ce){
        > myimg=new Image()
        > myimg.src=sourc e
        > }
        > cacheimage('clo sed1.jpg')
        >
        > Is this because the myimg object is destroyed as soon as the function
        > returns, or for some other reason? Does anyone have a simple, one[/color]
        argument,[color=blue]
        > void returning function with where you can give the url of an image and[/color]
        have[color=blue]
        > it precahed like this?
        >
        > Any answer must work in IE5+.
        >
        > Thanks!
        >
        >[/color]

        Try this:

        function cacheimage(sour ce){
        window['myimg']=new Image()
        window['myimg'].src=source
        }

        This way, myimg will be a property of the window object, thus global.

        Otherwise, although you do not the var declaration, myimg will be caught up
        in the function's namespace only.


        JW



        Comment

        • Janwillem Borleffs

          #5
          Re: image precaching


          "viza" <none@example.i nvalid> wrote in message
          news:ih9Wa.88$A m1.34@newsfep1-gui.server.ntli .net...[color=blue]
          > and then Lasse Reichstein Nielsen said:
          >[color=green]
          > > "viza" <none@example.i nvalid> writes:[color=darkred]
          > >> This does not seem to work:[/color]
          > >
          > > HOW does it not work? I.e., what did you expect to happen and what[/color][/color]
          really[color=blue][color=green]
          > > happens? And where do you put the code on a page?[/color]
          >
          > Try it:
          > http://homepage.ntlworld.com/l_vajzovic/testcase.html
          >
          > It seems to be working in Moz at home, but IE5.0 at work seems to be not
          > doing it.
          >[/color]

          Which is quite amazing, because the file is incomplete. I have edited it
          somewhat and now it works for me:

          <html>
          <head>
          <title>testcase </title>
          <script type="text/javascript">

          function cacheimage(sour ce){
          myimg=new Image()
          myimg.src=sourc e
          }
          cacheimage('2.j pg')

          window.onload = function () {
          document.getEle mentById('imgid ').onclick = function (){
          this.src=myimg. src;
          }
          }

          </script>
          </head>
          <body>
          <p>Click on the image to change it - it should happen instantly.
          <p style="backgrou nd:#dddddd"><im g alt="" src="1.jpg" id="imgid">
          </body>
          </html>


          JW



          Comment

          Working...