TIP: Remove alert() for polling application
TIP: Remove alert() for polling application
Preamble: For factors that are a dark mystery to me, my server would sometimes return an incorrectly-formatted JSON file. There are probably ways to sanitize and validate on the server side before sending out, but you won't be able to account for everything that could go wrong in a transfer. If you request your data thousands of times in an hour, something is going to break the JSON being returned somehow.
Problem: The current implemenation of DataTables uses a JavaScript alert to advise of the broken data. But since a polling application is going to fetch new data shortly anyhow, the pause created by alert() is probably unecessary. Yet, you may want to see what's going on anyhow.
The solution is to use console.log() instead of alert(), but this is not without its problems... in unsupported browsers, the script will stop executing altogether.
Best Solution (I have not done this one): create a new console() function that will process logic to use either console.log (if available) or a different mechanism (IE should have some sort of log in the window object) otherwise.
Weak Solution: simply locate the alert and remove it. There's still some internal logic, but it will fire an empty anonymous function that does nothing. The remaining logic is so tiny that you won't notice a performance hit. But I just don't like this since Webkit consoles won't inform you of the error. (Firebug consoles will continue to show you that something went wrong with the GET and you can drill into it)
Half-Assed Solution (the one I used): replace the alert() with a new nested 'if' statement that simply checks the typeof console. If there's no console available, the function returns "undefined". If there's a console, it's of type object. The code simply looks like this:
[code]if (typeof(console) != "undefined") console.log("JSON data from server could not be parsed due to formatting error.";[/code]
I call it half-assed because it only works in browser configurations that support console (Firefox with Firebug, Safari with Dev Tools enabled, Chrome). But that's good enough for my needs.
Problem: The current implemenation of DataTables uses a JavaScript alert to advise of the broken data. But since a polling application is going to fetch new data shortly anyhow, the pause created by alert() is probably unecessary. Yet, you may want to see what's going on anyhow.
The solution is to use console.log() instead of alert(), but this is not without its problems... in unsupported browsers, the script will stop executing altogether.
Best Solution (I have not done this one): create a new console() function that will process logic to use either console.log (if available) or a different mechanism (IE should have some sort of log in the window object) otherwise.
Weak Solution: simply locate the alert and remove it. There's still some internal logic, but it will fire an empty anonymous function that does nothing. The remaining logic is so tiny that you won't notice a performance hit. But I just don't like this since Webkit consoles won't inform you of the error. (Firebug consoles will continue to show you that something went wrong with the GET and you can drill into it)
Half-Assed Solution (the one I used): replace the alert() with a new nested 'if' statement that simply checks the typeof console. If there's no console available, the function returns "undefined". If there's a console, it's of type object. The code simply looks like this:
[code]if (typeof(console) != "undefined") console.log("JSON data from server could not be parsed due to formatting error.";[/code]
I call it half-assed because it only works in browser configurations that support console (Firefox with Firebug, Safari with Dev Tools enabled, Chrome). But that's good enough for my needs.
This discussion has been closed.
Replies
Nice solution though - there are always many ways. You could also refined alert if you wanted... :-) alert = function () {;};.
Allan
[code]
if (typeof(console) != "undefined") {
alert = console.log; // disable annoying JSON parse error alerts
} else {
alert = function () {
;
}; // if there's no console, just do nothing so that end-users aren't paused by alert
}
[/code]
I was having some functionality hindered by trying to just re-assign the function. In the above, typeof(console) ended up correctly being evaluated as undefined, so the condition passed and alert were redefined to console.log. But every now and then (in Chrome), trying to alert would result in an illegal invocation error that would break the rest of the script. Here's the fix I came up with:
[code]
// divert alerts to console.log if available
if (typeof(console) !== "undefined") {
window.alert = function(content) {
try {
window.console.log(content); /* send alerts to console.log if available. */
} catch(e) {} /* otherwise do nussink else */
}
}
[/code]
Allan