Search code examples
javascriptoperaonerror

javascript onerror function doesn't execute in the normal sequential flow?


my code is something like this:

function func1()  {

     document.getElementById("img").src="pic.jpg";
     ---stament2---;
}

document.getElementById("img").onerror="func2()";

In other browser, if there is no pic.jpg, func2() is executed and AFTER that, stament2 is executed; in opera if there is an error loading the image, func2() is executed after stament2.

Why did they did it like this? Is there any advantage? IS THERE A WAY to make it run the function called by onerror right when it's called and not after other statements?

if someone could please help....

Does jquery or other library have a crossbrowser solution for onerror applied to images? hmmm...must learn some jquery

thanks


Solution

  • You can put statement2 into a separate function and call it at the end of the onLoad of images as well as the end of the onError. Check out: Having troubles preloading images with javascript