Object.Onerror = Action
I have created a script that will deliberately create an error message, click Link One.
You will recieve an error message similar to the following.
Line: 18
Error: 'document.fiction' is null or not an object
URL: http:\\www.huntingground.freeserve.co.uk\scripts\error.htm
Now, by including window.onerror = Do_This in the script, instead of showing the error message function Do_This will be run..
Do_This can be any function name of your choosing.
I am using function Do_This to show the difference between having a Return value of True and a Return value of False.
Click Link Two.
Notice that the error box did not show itself, this is because the statement had a ReturnValue of true
Below is the script I used for this example.
<script type="text/javascript">
<!--
function test1(){ // link one
which_test="test1"
document.fiction.value=""
}
function test2(){ // link two
which_test="test2"
document.fiction.value=""
}
window.onerror = Do_This
function Do_This() { // error function
if(which_test=="test2"){
alert("This could be a secondary function")
return true // cancels the error message
}
else{
return false // shows the error message
}
}
// -->
</script>
Here's another example using images for an onmouseover effect.
Passing your mouse over image 1 will swap the image.
Passing your mouse over image 2 will invoke the onerror event because I have deliberately left out the image source so no image will load.
| Example 1 | Example 2 |
![]() |
![]() |
Notice the only difference between the two codes is the onmouseover src and whilst both include the onerror method only the second image invokes it..
<IMG src="red1.gif" alt="A Simple Mouseover Image Change" onmouseover="this.src = 'red.gif'" onmouseout="this.src = 'red1.gif'" onerror="alert('Oops I did not put in the image source.')">
<IMG src="red1.gif" alt="What! .... No Image?" onmouseover="this.src = ' ';" onmouseout="this.src = 'red1.gif'" onerror="alert('Oops I did not put in the image source.')">
Onerror creates an array of arguments
alert(arguments.length+'\n'+arguments[0]+'\n'+arguments[1]+'\n'+arguments[2])