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?

6
  • 2
    Put a try-catch around where their code executes. If they are directly binding to events, you are out of luck. Commented Feb 3, 2013 at 20:34
  • You could also try to detect if jquery is loaded before trying to add it. See here: stackoverflow.com/questions/1828237/… Commented Feb 3, 2013 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. Commented Feb 3, 2013 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? Commented Feb 3, 2013 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. Commented Feb 5, 2013 at 5:19

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!
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.

1 Comment

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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.