I'm writing scripts which I want to split into several modules. The "baseline" modules will support older browsers, which do not support new syntax such as === and promises.
The "advanced" modules will be loaded if the browser passes a feature-check.
My question is, how do I check if browser supports === operator and .then(function(){}) promise syntax without actually using them first, and causing a syntax error in older browsers?
if (/*what goes here*/) {
var script = document.createElement('script');
script.src = '/advanced.js';
script.async = false;
document.head.appendChild(script);
}
If a browser supports promises, it will support
then. One way (among others) to see if a browser supports promises (without throwing an error) would be to see ifwindow.Promiseexists:As for
===, I don't think you'll have to worry about that one.===was added to ECMAscript in the 3rd edition in December of 1999 and it is hard to imagine anyone (even a diehard laggard) using a browser today that doesn't support it.UPDATE:
If you really insist on detecting
===support, my conclusion (from my comments below) is to accomplish this by researching which browsers do not support===and using Browser Detection to detect those browsers. I hope someone else offers you an easier way I'm not thinking of.