2

I use the WP native function wp_enqueue_script() for all my script loading in both WP front and back-end so it can handle duplicated calls to the same script and so on.

One of the issues is that other programmers don't use this function and load their scripts directly from their code, which causes jQuery or jQuery-UI to be loaded twice, leading to a bunch of errors.

The other issue is that code not owned by me triggers an error and stops the execution of JavaScript beyond this point.

In short:

A Javascript error occurs in code not owned by me.
My code doesn't execute due to that error.
I want my code to bypass that error and still execute.
Is there a way to handle these issues?

MikeM
  • 13,156
  • 2
  • 34
  • 47
Diosney
  • 10,520
  • 15
  • 66
  • 111
  • 2
    Put a try-catch around where *their* code executes. If they are directly binding to events, you are out of luck. – techfoobar Feb 03 '13 at 20:34
  • You could also try to detect if jquery is loaded before trying to add it. See here: http://stackoverflow.com/questions/1828237/check-if-jquery-has-been-loaded-then-load-it-if-false – rsbarro Feb 03 '13 at 20:35
  • 1
    band-aid solutions are never good solutions. You'd be better off trying to determine why it's loading twice and stop it from doing so. – rlemon Feb 03 '13 at 20:51
  • 3
    Not all errors can just be "bypassed". Errors generally indicate that something is wrong that is _preventing the code from being executable_ or even from making any sense. How do you intend for the computer to automatically guess how to resolve that? – Lightness Races in Orbit Feb 03 '13 at 21:22
  • 3
    I think that the idea of the question is: if my scripts works properly and only requires the resources I load, how can I do to ignore others scripts errors (added by others plugins, for example)? My JS doesn't depend on them to work properly, so I don't want fix them, only want that my scripts gets executed. – leticia Feb 05 '13 at 05:19
  • @gsc-leticia: Exactly. That's the point. – Diosney Aug 13 '13 at 00:47

2 Answers2

6
function ShieldAgainThirdPartyErrors($) {
    // Code you want protect here...
}

// First shot.
// If no error happened, when DOMContentLoaded is triggered, this code is executed.
jQuery(ShieldAgainThirdPartyErrors);

// Backup shot.
// If third party script throw or provoke an unhandled exception, the above function 
// call could never be executed, so, lets catch the exception and execute the code.
window.onerror = function () {

    ShieldAgainThirdPartyErrors(jQuery);

    return true;
}

If you want pull the trigger of your gun twice just when necessary ;) set a flag to signal that the first shot was successful and avoid the backup shot, I think that under some circumstances your first shot could be executed even third party code get in trouble and trigger the second shot.

  • Althought I don't like completelly the idea since it leaves some gaps to errors (despite the flag signaling) I think as for now there is no other way to accomplish that, so I will certainly stick with your answer :D Thanks! – Diosney Aug 13 '13 at 00:56
0

Not at all recommended but you can use:

try {  
  //Some code likely to throw an error  
} catch (err) {}

This will allow anything after to continue running though you should generally catch the error too. The only case when I would not catch the error is when working with arrays and doing somearray[i-1] where the i-1 would be less than 0. This is only because it is cleaner than using conditionals to prevent it.

James Coyle
  • 9,922
  • 1
  • 40
  • 48
  • 1
    I would happily use `try-catch` if was my code the bad guy, but this time is other peers code that are the ones that are triggering errors, which can be catched by my local `try-catch`. – Diosney Aug 13 '13 at 00:49