Jason King Team : Web Development Tags : Web Development Tips & Tricks

Find JavaScript errors early!

Jason King Team : Web Development Tags : Web Development Tips & Tricks

As a web developer my favourite browser is Chrome.   It’s fast and has excellent developer tools.   Unfortunately unless you have the developer tools open it’s easy to miss subtle JavaScript errors.   By subtle I mean an error which doesn’t affect the functionality but is still an error.   To stop this from happening and be notified of any errors, I add the following code to the .NET master page (or Layout in an MVC website).

window.onerror

This means that for local requests (you wouldn’t want this code to run in production), if a JavaScript error occurs, it will be alerted to you on the screen so you can hang your head in shame…but also be thankful that you found it before you checked in the offending code.

This is also useful if you are using Selenium to automate your UI testing. Selenium will throw an exception if you try to find an element while an alert is displayed in the browser.