简体   繁体   中英

Run External Javascript On Page With JS Errors

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?

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 (eg 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM