Is there anyway by javascript to know if an image didn't load; I mean that an image didn't display.
Is there anyway by javascript to know if an image didn't load; I mean that an image didn't display.
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.
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.
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?
I'm actually trying to know that an image does NOT exist. I hope that makes sense...
Is there anyway by javascript to know if an image didn't load; I mean that an image didn't display.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?I'm actually trying to know that an image does NOT exist. I hope that makes sense...![]()
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
Can I connect this code with an image i already have coming in. I think your code creates a new image.
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.
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.
Bookmarks