Why I cannot handle js errors when adding a javascript code dynamically?
Here is the code:
try {
var element = document.createElement("script");
element.language = "javascript";
element.type = "text/javascript";
element.defer = true;
element.text = "this is not a javascript code";
var head = document.getElementsByTagName('head')[0];
head.appendChild(element);
} catch(err) {
alert("error caught");
}
The error caught
alert isn't shown even if the script is incorrect.
As far as I know, there's no way to handle errors (even syntax ones) on certain script tag.
You could use window.onerror and look for SyntaxError
at the beginning of the error message. I suppose, this is the kind of errors, you're trying to catch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With