0

I'm writing an on-page chat software and I'm relatively new to these external JS issues.

My question is how can I load and run our JS on an external page and ignore / run before any existing JS errors on the given page.

Example : Our snippet is within the footer of the customers page. They release an update to one of their JS scripts and they fat finger some code and it breaks. Now our JS code will not load.

What is the best way to handle this?

Chris Favaloro
  • 79
  • 1
  • 12
  • duplicate: [Ignore javascript errors in a page and continue executing the script](http://stackoverflow.com/q/12219154/2033671) –  Mar 01 '15 at 21:04

2 Answers2

0

Use a separate <script> element for your script. Each script will run independently (although they will do so in a shared environment).

If you are depending on their script running successfully (e.g. to provide you with functions or elements in the DOM), then you need to program defensively and test for things you depend on before using them. How you handle those errors is up to you, you could substitute your own replacements, throw your own error, and so on.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Essentially at the moment the script is dynamically generated like so : `` The issue seems to be if they put this in the footer of their website and a script before this throws an error, some existing AJAX requests seem not to fire. – Chris Favaloro Mar 01 '15 at 21:35
-1

You can't. If the browser has thrown an error earlier, it will never get to your code. The best you can do is putting a <noscript> tag in to explain that something went wrong.

If you get the customer to put your script tag in the head (before their code) and mark it as async, it might still run, but I wouldn't depend on it.

Jens Neubauer
  • 1,090
  • 1
  • 13
  • 24
  • If it is a separate script tag (as your last paragraph seems to imply), then your first statement isn't true. Putting a ` – Quentin Mar 01 '15 at 21:14
  • Alright, the noscript thing was a bit of a brain fart on my part. However, separate script tags do not guarantee that the later script will run in all cases. In particular, older versions of IE can and will break the entire page. – Jens Neubauer Mar 02 '15 at 22:37