I’m including some 3rd party code in my application and I see that it uses document.write() code which is wrapped in a try..catch statement.
The document.write() doesn’t return anything, so I wanted to know is there a way to test if document.write() was successful, so I can test catching the error.
As an example, I tried including this script async but this stopped the document.write() part executing but it didnt run the code in the catch() part.
example code:
try {
document.write('<script type="text/javascript" src="/plugin.js">');
console.log('OK');
} catch (e) {
console.log('ERROR');
}
You can add id to script tag and then fetch it from DOM by id:
or you can try to find it in all scripts on page if you don’t want to change anything: