52

I develop plugins for WordPress. It uses some jquery in the user side (themes) as a jquery plugin. The problem is, when there is an javascript error with other plugins made by other autors, my plugin's javascript fails to execute.

And the worst thing is people consider that there is a serious fault with my plugin even though it works 100% fine with error handling conditional statements. But it is actually due to some other javascript syntax errors of some other WP plugin/theme authors.

Is there a way to continue execute my plugin JS ignoring other JS errors. Or can i have suggestions to handle this problem ??

enter image description here

6
  • 3
    You wish, but there's always try/catch ??
    – adeneo
    Aug 31, 2012 at 16:21
  • 22
    You cannot catch what's not yours.
    – raina77ow
    Aug 31, 2012 at 16:26
  • 16
    @raina77ow - You should make dev-themed fortune cookies. Aug 31, 2012 at 16:27
  • @raina77ow - you got that right, LOL! But if your plugin has a conflict, it's usually something you did.
    – adeneo
    Aug 31, 2012 at 16:27
  • 1
    most errors appear in the ready event, my suggestion would be to use your own $(function() { equivalent so that your callbacks will work
    – Alex K
    Sep 18, 2012 at 20:47

11 Answers 11

25

Try :

window.onerror = function(){
   return true;
}

That is if you can include this before the bugged script has been executed.

8

You should correct the old JavaScript error because it may create many problems, not for right now but for next time.

Put your JavaScript file / code at the top (before JS having error), and call it before JavaScript effected by other JavaScript code.

In case you need handle JavaScript exception at run time, best option is

try { /* run js code */ } 
    catch (error){ /* resolve the issue or bug */ }
4
  • 5
    Actually, the problem is created by other author's JS file. So i don't know how to try catch it, and in WP there are lots of plugins and it is difficult to make my plugin load at first. Aug 31, 2012 at 16:33
  • 1
    Do not use "try catch" block in production, as it is NOT recommended and also not widely supported.
    – MiKE
    May 30, 2017 at 8:55
  • @MiKE link to more complete explanation of this? Aug 22, 2022 at 20:48
  • 1
    @Nathan my comment was made in 2017 so I guess the support is now great. You can check it here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – MiKE
    Aug 31, 2022 at 10:02
4

You should be able to swallow any error using the error event:

$(window).error(function(e){
    e.preventDefault();
});

I've never attempted this, but it should work in theory.

It should be noted that this probably isn't a great solution to your problem. It could be that your plugin is interfering with other plugins. It is, of course, possible that the errors are no fault of your own, but generally speaking (with publicly released plugins) not the case.

1
  • 1
    api.jquery.com/error - it's deprecated, and anyway, the recommendation is to not use this against window.
    – ScottE
    Sep 11, 2012 at 1:50
4

I encountered the same problem: There are a bunch of plugins out there that make too many assumptions and cause JavaScript errors on YOUR page, even when you are being a good citizen; these errors have nothing to do with your plugin.

There's no sense trying to handle errors in other plugins -- IMO it is better to be self-contained but resilient to their errors.

In my case, the errors were halting the jquery DOM ready event, and my JavaScript init code wasn't getting executed. The exact form of the error isn't important though -- the solution is just to fire on multiple events.

The solution for me was to have fallbacks in addition to relying on the jQuery DOM ready event:

  1. I wrapped any code I wanted to fire on the DOM ready event into their own function -- e.g. my_hardened_init_action();
  2. I added a function, my_hardened_init(), that only runs once. It calls my_hardened_init_action() the first time it is called, and does nothing on subsequent calls.
  3. I added various methods to call my_hardened_init() in the WordPress footer. In my case, I only needed two. First, trying the usual jQuery DOM init, but falling back to a simple setTimeout(). So if the jQuery DOM init never fires due to broken JavaScript, the timeout will fire shortly after when the page has finished loading.

You could add multiple other fallbacks -- even add the code to the header if needs be. As my_hardened_init() only runs once, you can try as many times as you like to trigger it.

This has worked on a bunch of client sites with a range of other broken plugins.

Hope this helps.

0
2

What about window.onerror?

https://developer.mozilla.org/en-US/docs/DOM/window.onerror

1
<script>
var _z = console;
Object.defineProperty(window, 'console', {
    get: function(){
        if (_z._commandLineAPI) {
         return true;   
        }
        return _z;
    },
    set: function(val) {
        _z = val;
    }
});
</script>
1

If you page is running. Use:

$(window).error(function(e){
        e.preventDefault();
});  // IGNORE ALL ERROR JQUERY!

or use:

window.onerror = function(){
   return true;
}  // IGNORE ALL ERROR JAVASCRIPT!
0

A regular try catch statements won't work for these types of errors.

Possible workarounds:

Use inline-css to float the bar to the left instead of jQuery (I am not sure what the full function of your plugin is but if it just floats the bar to the left why not just use css?)

Have a invisible iframe which tries to run some code on the parent page. if it fails than alert the user(from within this iframe) that it wasn't your fault :)

0

Perhaps you could try putting your js in an html object, so that it's executed in a separate page. That probably won't help much if the js on the main page wont run to interact with the object. Just something to play with.

2
  • It isn't very clear what you're trying to say. Can you reword it so it's clearer? Sep 18, 2012 at 22:56
  • Sorry, I had some html in there that was screwing up the post. Sep 20, 2012 at 4:30
0

Put your call to plugin in

try{
  ......
}
catch(e){

}
0

This is worked for me:

try{
    //your code
}
catch(error)
{
    return true;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.