Reputation: 956
When the user user old browsers like (IE 6/7/8) then the page should show a warning message and inform user to upgrade the browser.
But in internet, there are few articles suggesting not to detect browser. But detect the feature support.
For E.g when working with localstorage.
if(localStorage) {
// Perform actions
}
But is this correct? If we need to detect feature then we need lots of conditions involved.
Also how to get the browser version using javascript/jQuery ? Also I need to know whether the user uses the compatibility mode as well
Upvotes: 0
Views: 311
Reputation: 324750
Always, ALWAYS feature-detect.
There are hundreds of browsers out there. Not just on computers, but on phones, tablets, games consoles...
The Nintendo DS browser doesn't support localStorage
, but if you're only excluding old IE then Nintendo DS users will be unable to use your site and not know why.
So unless you feel like compiling a list of every single browser that has ever been developed, and checking if they have the features you want, and building a long list of unsupported browsers... just save yourself the trouble and write if(!window.localStorage) { /* implement some fallback for localStorage here, maybe cookies? */ }
Upvotes: 2
Reputation: 3817
Trying to detect a browser is complicated and tricky, because there's just so many. Feature detection is a much "cleaner" way, because you can make use of all the individual features that you know are available, without having to make assumptions.
In the case of checking for localStorage availability, you should actually check like this:
if(typeof window.localStorage !== "undefined"){
If a variable doesn't exist, an error will be thrown, so the else
condition of your statement would never run. Because localStorage
is a property of window
, you can check that the property exists and get a boolean.
Upvotes: 0