Home arrow JavaScript arrow JavaScript Errors

JavaScript Errors

When last we spoke, we discussed the various Events in JavaScript. In this tutorial we will go over some of the errors that can occur and how to deal with them. I know, I know: how can any of your programs possibly fail when you have been taught by the JavaScript Ninja? Well, you can blame that on the good folks that create the various browsers.

Author Info:
By: James Payne
Rating: 4 stars4 stars4 stars4 stars4 stars / 12
January 07, 2008
  1. · JavaScript Errors
  2. · Try...Catch
  3. · Throwing the Ball (and Exceptions)
  4. · By the Numbers

print this article

JavaScript Errors
(Page 1 of 4 )

Old Schooling it with the OnError Event

I believe we covered this briefly in our last series, but we will do so a little more in depth here. The OnError event occurs whenever there is a script error on the page. To use the event, you create a function to deal with the error. When the error occurs, the function is called by the Onerror event handler, which consists of three arguments: the error message (msg), the URL of the page where the error occurred (url), and the line where the error occurred (line). Here it is in code:



<script type="text/javascript">


var txt=""

function handletheerror(msg,url,l)


txt="An error has occurred.nn"

txt+="Error: " + msg + "n"

txt+="URL: " + url + "n"

txt+="Line: " + l + "nn"

txt+="Click OK to continue.nn"


return true


function message()







<input type="button" value="Click to trigger the error" onclick="message()" />



In the code above, we create a function named handletheerror that will occur when the Message function fails to work (note that this function fails to work because the adddblerto should actually be code for an alert, but has many typos; if you fixed the code, the onerror event would never occur). It triggers a pop-up box that tells you an error has occurred, the URL at which it occurred, and on which line in the code the error appears.

Note that when counting lines, JavaScript includes blank lines as well.

blog comments powered by Disqus

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials