Page 1 of 4 123 ... LastLast
Results 1 to 10 of 36

Thread: Anyway to know if an image did NOT display in the browser?

  1. #1
    Join Date
    Jan 2009
    Posts
    82
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Anyway to know if an image did NOT display in the browser?

    Is there anyway by javascript to know if an image didn't load; I mean that an image didn't display.

  2. #2
    Join Date
    Jan 2009
    Posts
    82
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    You didnt do that to to mess with me right??

    I tried that and kept getting and alert that said nope that wouldn't go away unless I forced the system to quit the browser..

    Anyway, what I'm really trying to do is actually verify that an image does exist and does in fact load.

    I see you now deleted your post.

  3. #3
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Sorry about that. I didn't test the code before I posted it. After I posted it, I tested and it didn't work (go figure ). Something similar to the code I posted may work. I know that attaching the 'onload' event to an image can do something when it loads, but this has to be done via javascript. I tried, but I failed. If you still want the code, here is the browser-compatible "addEvent" function:

    Code:
    function addEvent(el, ev, response) {
    	  var elem = document.getElementById(el);
              if (elem.addEventListener)addEventListener(ev, response, false);
          else if (elem.attachEvent) {
            var res = function() {
              response.apply(elem);
            };
            elem.attachEvent("on" + ev, res);
          } else elem["on" + ev] = response;
        }
    Edit: WHERE I'M STUCK: To make sure that an element exists, you have to attach the event in a window.onload function. Problem: the image may already be loaded when window loads. Javascript wouldn't parse in time, thus the image that you want to check is loaded is already loaded or may already be loaded and you can't attach an onload event to something that has already loaded... If the image is a larger file, it could possibly work. Now, the way you would want to avoid this is make it attach the onload event to the element before it loads. You then wouldn't place one of those functions into the window.onload function. This would parse BEFORE the window loads. Problem: the element wouldn't even EXIST before the pages loads -.- I don't see any other way to do this.

  4. #4
    Join Date
    Jan 2009
    Posts
    82
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Oh its ok.

    I'm not sure how to use the code you posted in relationship with the image. I'm not fully sure if you meant that the code works or does something else?

  5. #5
    Join Date
    Jan 2009
    Posts
    82
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    I'm actually trying to know that an image does NOT exist. I hope that makes sense...

  6. #6
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Is there anyway by javascript to know if an image didn't load; I mean that an image didn't display.
    I'm actually trying to know that an image does NOT exist. I hope that makes sense...
    HOLD IT! You want to check if an images EXISTS? NOT if an image HASN'T LOADED? Or vice versa? Or even BOTH? Or check to see if it loads to therefore see if it exists?

  7. #7
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    How about this?:
    Code:
    <script type="text/javascript">
    var newImage = new Image();
    newImage.src = "image.png";
    newImage.onerror = function(){
      alert("Your picture doesn't exist. :(");
    };
    </script>
    Jeremy | jfein.net

  8. #8
    Join Date
    Jan 2009
    Posts
    82
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Can I connect this code with an image i already have coming in. I think your code creates a new image.

  9. #9
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Please post a link to the page on your site that contains the problematic script so we can check it out. This helps us help you.

    Haha- I couldn't resist.

    If you do not have a website, please post your code so we can check it out.

  10. #10
    Join Date
    Jan 2009
    Posts
    82
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    There's no problematic script yet. I jsut have a a user generated image:
    <img src="whatever.jpg" />

    I just want to know if the image exists or not and put a message in place.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •