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;
}
如果您想在必要时扣动您的扳机两次;)设置一个标志以表示第一次射击成功并避免备用射击,我认为在某些情况下,即使第三方代码进入,您的第一次射击也可以执行麻烦并触发第二枪。