0

I will try to explain cleary what I'm looking for because I think my title is awful.

Sometimes, when you try to execute some JavaScript code in your own plugin, it will just break because somewhere else, there is an error in a JavaScript execution.

Few months ago, I found a way (on those forums) to "close" or "end" the previous Javascript execution in error, in order to guarantee your own js file to be executed correctly.

It was some caracter to insert at the beginning of your js file, like )]}; but I can't find it back.

Thanks in advance,

Best Regards

Gio
  • 543
  • 1
  • 5
  • 10
  • What are you tring to do? Writing a browser plugin in javascript? For which browser? – Marcel Nov 21 '17 at 10:01
  • Perhaps you meant an IIFE? `(function() { /* your code */ })()` – mplungjan Nov 21 '17 at 10:03
  • I write some plugin in html/javascript, some kinds of functionnalities that someone can include in his website. But if one of his js files have an error, it won't continue executing all the js files. – Gio Nov 21 '17 at 10:04
  • Please see here https://stackoverflow.com/questions/7365172/semicolon-before-self-invoking-function This might help you. – Vipin Kumar Nov 21 '17 at 10:04
  • Or https://stackoverflow.com/questions/7145514/whats-the-purpose-of-starting-semi-colon-at-beginning-of-javascript – Vipin Kumar Nov 21 '17 at 10:05
  • If i'm correct, any unhandled error would stop any further javascript code to be executed BUT in same `script` context. So if you have unhandled errors in your script, there is nothing to do except trying to catch these errors. If error comes from other `script`, you have nothing to do to let your own 'plugin' to be processed. And if you have still some issue, you could still try to catch any error globally: https://stackoverflow.com/a/10556743/1414562 – A. Wolff Nov 21 '17 at 10:09
  • The character you are looking for is `;` which you find often at the beginning of some plugins. You can take a look to this questions and answers : https://stackoverflow.com/questions/17978883/what-is-the-purpose-of-a-semicolon-before-an-iife https://stackoverflow.com/questions/1873983/what-does-the-leading-semicolon-in-javascript-libraries-do – ADreNaLiNe-DJ Nov 21 '17 at 12:34

1 Answers1

0

You wrap your code in a IIFE (immediately invoked function expression). To make things even safer, start it with a semi-colon.

;(function(){
  // your code here
})()

Code inside the IIFE can now run safely. If it crashes the rest of the code will still run. Note that the entire code must still parse into JavaScript.

Ryan Taylor
  • 12,559
  • 2
  • 39
  • 34