Jump to content

Why doesn't onerror work when the image is....


Aphanasiy
 Share

Recommended Posts

Hello folks, could you please explain to me why this script doesn't work?

<span style="background-image:url('http://www.badimage.gif')" onerror="alert('testing')">test</span>

Onerror is supposed to work when my browser downloads an image, if it can't find one the onerror function is fired. Why doesn't it work here when it works in this script?

<img src="http://www.badimage.gif" onerror="alert('testing')">

Link to comment
Share on other sites

Because the <span> does not have an error...the style attribute does if it doesn't load the background.Loading an image and loading a background image are different and obviously trigger events differently.AFAIK there is no way to catch an error when a bg doesn't load.

Link to comment
Share on other sites

Because the <span> does not have an error...the style attribute does if it doesn't load the background.Loading an image and loading a background image are different and obviously trigger events differently.AFAIK there is no way to catch an error when a bg doesn't load.

So how do I trigger an onerror function using the style attribute please?Edit: Sorry for double post.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...