Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. Is it possible to detect if an image file really exists on the drive with javascript?

Is it possible to detect if an image file really exists on the drive with javascript?

Scheduled Pinned Locked Moved Web Development
javascripthelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    davilism
    wrote on last edited by
    #1

    I want to load all the images with the specific name and ended with a digit like image1.jpg, image2.jpg, image3.jpg, etc. The problem is that I wouldn't know in advance how many of them, so I just keep looping until I am told there is no more file.

    A 1 Reply Last reply
    0
    • D davilism

      I want to load all the images with the specific name and ended with a digit like image1.jpg, image2.jpg, image3.jpg, etc. The problem is that I wouldn't know in advance how many of them, so I just keep looping until I am told there is no more file.

      A Offline
      A Offline
      alex barylski
      wrote on last edited by
      #2

      I recall doing something like this before. As long as you know the names of the files i believe you can use this technique on both server or client side. The IMG tag has a fileSize property which is read only. So what I would do ischange the IMG SRC dynamically and capture the onload the IMG fires when it loads any image, etc. In the onload handler check the fileSize property. If it's Zero or negative (I can't remember) the file doesn't exist or didn't load properly. HTH It's frustrating being a genius and living the life of a moron!!!

      D 1 Reply Last reply
      0
      • A alex barylski

        I recall doing something like this before. As long as you know the names of the files i believe you can use this technique on both server or client side. The IMG tag has a fileSize property which is read only. So what I would do ischange the IMG SRC dynamically and capture the onload the IMG fires when it loads any image, etc. In the onload handler check the fileSize property. If it's Zero or negative (I can't remember) the file doesn't exist or didn't load properly. HTH It's frustrating being a genius and living the life of a moron!!!

        D Offline
        D Offline
        davilism
        wrote on last edited by
        #3

        That's one good technique but if it's used in a loop, the time it takes to load the image is certainly longer and the size it finds must be 0. How do I get around this?

        A 1 Reply Last reply
        0
        • D davilism

          That's one good technique but if it's used in a loop, the time it takes to load the image is certainly longer and the size it finds must be 0. How do I get around this?

          A Offline
          A Offline
          alex barylski
          wrote on last edited by
          #4

          It's the only technique you can use when all you have at your disposal is a javascript. If you can use PHP or ASP, then it's obviously much better to build the array of files and output them as an JScript array. It doesn't sound like you can do this??? davilism wrote: longer and the size it finds must be 0. How do I get around this? Get around what? The fileSize attribute should return the size in bytes of the file. If it doesn't, then something went wrong (i think) One thing I do remember, is the IMG tag must NOT have it's dimensions set explicitly and I don't think it can be invisible. Cheers :) It's frustrating being a genius and living the life of a moron!!!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups