Ignore javascript syntax errors on page and continue script execution

I develop plugins for WordPress. It uses some jquery in the user part (theme) as a jquery plugin. The problem is that when there is a javascript error with other plugins made by other authors, my javascript of my plugin cannot execute.

And the worst thing is that people believe that there is a serious error in my plugin, even if it works 100% perfectly with conditional error handling statements. But actually it is connected with some other javascript syntax errors of some other authors of WP plugins / themes.

Is there a way to continue executing my JS plugin while ignoring other JS errors. Or do I have suggestions for solving this problem?

enter image description here

+44
javascript jquery html plugins wordpress
Aug 31 '12 at 16:20
source share
10 answers

Try:

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

That is, if you can enable this before the script you have listened to is executed.

+22
Sep 17
source share

You have to fix the old JavaScript error because it can create a lot of problems, not now, but next time.

Place your JavaScript file / code at the top (before JS gets the error) and call it before JavaScript executed by other JavaScript code.

If you need to handle JavaScript exception at runtime, the best option is

 try { /* run js code */ } catch (error){ /* resolve the issue or bug */ } 
+8
Aug 31 '12 at 16:28
source share

You can learn any error using the error event:

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

I have never tried this, but it should work theoretically.

It should be noted that this is probably not a great solution to your problem. Your plugin may be interfering with other plugins. Of course, it is possible that the errors are not your mistakes, but, generally speaking, (with publicly released plugins) is not so.

+4
Aug 31 '12 at 16:52
source share

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

It makes no sense to try to handle errors in other plugins - IMO is better to be self-sufficient, but resistant to their errors.

In my case, errors stopped the DQLOM ready event, and my JavaScript initialization code did not execute. The exact form of the error is not important, although the solution is to simply fire several events.

The solution for me was to have backups, rather than relying on the JQuery DOM ready event:

  • I wrapped any code that I wanted to run in the finished DOM event in my own function - for example. my_hardened_init_action ();
  • I added the function my_hardened_init (), which runs only once. It calls my_hardened_init_action () on the first call and does nothing on subsequent calls.
  • I have added various methods to call my_hardened_init () in the WordPress footer. In my case, I was only two. First try the regular jQuery DOM init, but back to the simple setTimeout (). Therefore, if the jQuery DOM init never fails due to JavaScript malfunctioning, the timeout fires shortly after the page has finished loading.

You can add several other backups - even add code to the header if necessary. Since my_hardened_init () is run only once, you can try as many times as you want to call it.

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

Hope this helps.

+3
Dec 31 '13 at 7:10
source share
+2
Sep 11
source share

If the page is running. Using:

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

or use:

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

+1
Jul 28 '14 at 16:28
source share
 <script> var _z = console; Object.defineProperty(window, 'console', { get: function(){ if (_z._commandLineAPI) { return true; } return _z; }, set: function(val) { _z = val; } }); </script> 
+1
Aug 11 '14 at 12:53 on
source share

Regular catch catch statements will not work for these types of errors.

Possible workarounds:

Use inline-css to float the panel on the left, not jQuery (I'm not sure what the full function of your plugin is, but if it just floats on the panel on the left, why not just use css?)

Have an invisible iframe that tries to run some code on the parent page. if it fails, than will warn the user (from within this iframe) that this is not your mistake :)

0
Sep 11
source share

Perhaps you could try placing js in an html object so that it runs on a separate page. This probably will not help if js on the main page does not work to interact with the object. Just what to play with.

0
Sep 18 '12 at 20:38
source share

Put your call to the plugin in

 try{ ...... } catch(e){ } 
0
Sep 22 '12 at 4:22
source share



All Articles