AJAX TipsOnerror event is one of the many functions developers have to exhaustively use if they are building highly interactive Ajax or JavaScript based application. This form of error handling in JavaScript aids developers in properly handling unexpected errors on events or when an expected form of data has not been received.
Aside from instructing the browser on the options available when error happens; onerror could be used by developers to know more about the application. This could be used to detect bugs in the application so that a remedy could be proposed as soon as possible.
The basic coding for onerror event goes this way:
window.onerror=function()
From this point developers have a wide variety of options on what to do whenever an error is detected. The options could range from a simple window display of error to an alternative function which will somehow remedy the error.
The former solution is often used when an unexpected but tolerable result has been received while the latter is used to remedy the error as soon as possible. The function after the onerror event could even be a combination of the two wherein a prompt will go out first and the user will be redirected to a page or a function.
As already indicated, the onerror function is not only intended in providing options for developers whenever an error occurs. This form of error handling function could be used so that developers will have more information about the said error.
To enable more data whenever an onerror event occurs, developers simply have to add parameters in the function. Instead of placing a simple error message, developers could request more data from the browser. Basically, the data that could be extracted from the onerror event is in knowing where the error occurs. Although this data is somehow limited, it eases the problem of developers as it has specifically pointed out where the error has occurred
The following are the parameters that could be added in the onerror event. Please do take note that these parameters have to in order:
• msg (the actual error message)
• url (the location of the error in terms of URL)
• line number (the specific location on the said URL)
There are times wherein the returned message from the function should be accepted but an onerror event has been implemented and the function has literally stopped.
This could be overridden by developers by simply adding “return true” after the said onerror event function.
For example:
window.onerror=function()
return true
With that coding, the application should go through and interpret the data provided as it is.
But you need to be careful in using this function. Although you can override data, the data might not be interpreted by other functions since the data has not been assigned to any recognizable format or result.
Onerror event is a highly useful function that helps developers monitor every error activity in their application. With proper use of the function and controlled implementation of “return true,” rapid improvement of the application could be achieved.