Auto-detect javascript errors and warn theme user

This may sound simple, but it’s not 🙂

What I want, and most likely many theme developers do (themes that use javascript / jquery), is to warn the theme user that his site has javascript errors.

Read More

In my themes at least, in about 95% of these error situations, the problem comes from outdated / badly-written installed plugins. The other 5% comes from conflicts between up-to-date plugins and the theme. So my message would suggest the user to check his plugins one by one and fix the issues.

I found a solution for this, but only for the dashboard (in my theme settings). Essentially what I’m doing is loading the home page inside an iframe and wait 5 seconds for a function inside the iframe’s document.ready() to send me the “OK” message. If it doesn’t come, I display the error (I’m using postMessage to communicate with the iframe document).

Anyway, my question is how can I do this from the front page too?
Without using pm, because it’s too heavy (in the theme settings it’s ok, because it’s used for live design tweaks too)

Other unreliable solutions:

  • add the message just after <body>, and hide it with javascript (with js after the message); the problem is that errors might come up after the check
  • add the message anywhere in the HTML, and remove it with jQuery on document.ready; unacceptable because the user sees the message for 2-3 seconds, during which the site is loaded…

Related posts

Leave a Reply

2 comments

  1. Why not just use the browser’s “developer” tool to display the errors and the code that caused the error? I know for a fact that IE, Firefox and Safari each have developer tools available (I use them all the time) – I don’t know about other browser’s capabilities.

  2. Just did a quick search to see if JavaScript has a global error handling function which you could use and found window.onerror. So I haven’t looked any further into it to see if it’s well supported, but presumably you could use the window.onerror function to send an ajax request to php which could send page,browser, probably failed script info etc. then use php to email the admin email this info. That way it could be triggered by any user viewing the site.