Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore javascript syntax errors in a page and continue executing the script

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

like image 848
Aakash Chakravarthy Avatar asked Aug 31 '12 16:08

Aakash Chakravarthy


People also ask

How to avoid syntax error in JavaScript?

How to Handle SyntaxError. Syntax errors in Javascript cannot be handled by using try-catch blocks as they are thrown while the code is being parsed. The window. onerror() function can be used instead to figure out that there is a syntax error.

What are syntax errors in JavaScript?

An exception caused by the incorrect use of a pre-defined syntax. Syntax errors are detected while compiling or parsing source code. For example, if you leave off a closing brace ( } ) when defining a JavaScript function, you trigger a syntax error.


2 Answers

Try :

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

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

like image 97
Cosmin Avatar answered Sep 25 '22 01:09

Cosmin


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 */ } 
like image 43
Krishna Kumar Avatar answered Sep 25 '22 01:09

Krishna Kumar