Suggested eval
way will give false negatives for CSP errors because they are not handled. If this is a problem, CSP errors can be handled as shown in this answer.
It is possible to do that, but the solution is not pretty at all and involves external script. The script can set a flag, or global error handler can be set up to watch for syntax errors when the script is loaded.
If the script results in syntax error, it is still considered loaded, so network problems cannot cause false positives:
async-detection.js
window.isAsyncAvailable = true;
async () => {};
main.js
new Promise(function (resolve, reject) {
var script = document.createElement('script');
document.body.appendChild(script);
script.onload = resolve.bind(null, true);
script.onerror = reject;
script.async = true;
script.src = 'async-detection.js';
})
.then(function () {
console.log(window.isAsyncAvailable);
})
.catch(/* generic script loading error */);
This method can be used to detect syntactic features used on the website that cannot be polyfilled or normally caught with try..catch
. It is particularly useful to reliably trigger Your browser is outdated nag screen.