Instead of using prefixes I want to ask site visitors to upgrade their browser
I'm re-building a site using CSS flexbox.
In checking browser compatibility, I see that flexbox is supported by all modern browsers, except that Safari 8 and IE 10 require vendor prefixes.
In checking Google Analytics, I see that 96% of site visitors in the last 6 months use browsers that fully support flexbox. The remaining 4% use browsers that require prefixes or provide no support.
Since we're talking about 4% of users, and the number will keep getting smaller, (and I like to keep my code as clean and simple as possible), I'm considering not using prefixes, and instead asking users to upgrade their browsers.
How can I target older browsers in order to display a message to users asking them to update their browser?
Here's what I have so far:
<!--[if IE]>
<div class="browserupgrade">
<p>You are using an outdated browser. Please <a href="http://browsehappy.com/">
upgrade your browser</a> to improve your experience.</p>
</div>
<![endif]-->
This IE conditional comment covers me for IE versions 6, 7, 8 and 9.
These visitors will get an alert with a link to download a current browser. However, Microsoft discontinued support for conditional comments starting with IE10.
Now I need something similar for:
- IE 10
- Safari 7-8
- Opera Mini < 8
- UC Browser for Android
- Android Browser < 4.4
Is there a simple JS/jQuery script to handle this job? Or another lightweight method?
Solution
Thanks for all the answers. Clearly there are many ways to tackle this problem (Modernizr, PHP, jQuery functions, plain Javascript, CSS, browser-update.org, etc.) Many of these methods will do the job completely and effectively.
I went with the simplest one: CSS (credit @LGSon).
This CSS covers essentially all targeted browsers, except for IE <= 7.
.browserupgrade { display: block; }
_:-ms-fullscreen, :root .browserupgrade { display: none; }
:-o-prefocus, .browserupgrade { display: none; }
@supports (display: flex) { .browserupgrade { display: none; }}
See the answer for details.
And for those relatively few visitors using IE <= 7, a conditional comment in the HTML:
<!--[if lte IE 7]>
<div style=" ... inline styles here ...">
browser upgrade message here
</div>
<![endif]-->
Revised answer after question edit
Here is a CSS only way to achieve that.
As the CSS @supports
won't work on your targeted (unwanted) browsers: Safari 7-8, IE <= 10, Android Browser < 4.4, UC Browser for Android and Opera Mini < 8, your "browserupgrade" message will be visible on those using this rule.
@supports (display: flex) { .browserupgrade { display: none; }}
There is a few browsers that still does support the non-prefixed flex
but doesn't support @supports
, IE 11(1) and Opera Mini 8, but luckily we can address them with a couple of CSS specific rules.
/* IE 11 */
_:-ms-fullscreen, :root .browserupgrade { display: none; }
/* Opera Mini 8 */
:-o-prefocus, .browserupgrade { display: none; }
Here is the complete code to show an upgrade message for your targeted browsers.
CSS
.browserupgrade { display: block; }
/* IE 11 */
_:-ms-fullscreen, :root .browserupgrade { display: none; }
/* Opera Mini 8 */
:-o-prefocus, .browserupgrade { display: none; }
/* all modern browsers */
@supports (display: flex) { .browserupgrade { display: none; }}
HTML
<div class="browserupgrade">
<p>You are using an outdated browser. Please <a href="http://browsehappy.com/">
upgrade your browser</a> to improve your experience.</p>
</div>
(1): The IE 11 CSS rule should work on IE Mobile 11 too, though haven't one to test it on.
The CSS @supports
is also available as an API, CSS.supports(). Here is a very well written article by David Walsh.
Additionally, if one would like to automatically redirect those browser, here is a small script that does that, after a delay of 10 sec.
var el = document.querySelector('.browserupgrade');
if (window.getComputedStyle(el,null).getPropertyValue("display") != 'none') {
setTimeout(function(){
window.location = 'http://browsehappy.com/';
}, 10000);
}
PREMISE
The JavaScript style
property returns a complete collection of CSS properties that the browser supports for the specified element, which can be tested using the following Snippet:
for(var x in document.body.style)
console.log(x);
This is true whether or not a particular property is explicitly set for the specified element. This can be tested by running the following Snippet in Chrome, for example; the first line will return false
as Chrome does not yet support the unprefixed appearance
property while the second line will return true
.
console.log("appearance" in document.body.style);
console.log("-webkit-appearance" in document.body.style);
body{
appearance:none;
}
However, it should be noted that if an unsupported property is set on an element using JavaScript, via the style
property then checking for the presence of that property will return true
:
document.body.style.appearance="none";
console.log("appearance" in document.body.style);
Therefore we will be using document.createElement()
to create a temporary element so we can be sure none of the properties we are checking for have been set in this manner. (Thanks to Gothdo for this suggestion, which removed the need for any assumptions to be made.)
REQUIREMENTS
Looking at the list of browsers to be targeted:
- Internet Explorer 10 supported flexbox with the
-ms-
prefix. - Safari 7 & 8 supported flexbox with the
-webkit-
prefix. - Opera Mini, according to caniuse.com, has supported flexbox without prefixing since version 5, however I cannot attest to the accuracy of that and am looking for someone to confirm it.
- UC Browser currently (v9.9) supports flexbox with the
-webkit-
prefix. - Prior to v4.4, Android Browser supported flexbox with the
-webkit-
prefix.
SOLUTION
We can see from that list that all the browsers to be targeted require prefixing of the flexbox properties so we can target them simply by checking for the presence of any of the unprefixed properties in the temporary element's style
collection. The following Snippet will return true
when run in any of the above browsers:
console.log(!("flex" in document.createElement("p").style));
Armed with this knowledge we can now create a working solution to display the browser upgrade message to the necessary browsers.
if(!("flex" in document.createElement("p").style))
document.getElementById("browserupgrade").style.display="block";
#browserupgrade{
display:none;
font-family:sans-serif;
}
<div id="browserupgrade">
<p>You are using an outdated browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
</div>
NOTES
Old Syntax
There was a version of the current flexbox spec that used a slightly different syntax with one of the differences being that the order
property was named flex-order
. To the best of my knowledge, the only browser that ever supported that syntax was IE10 which required prefixing so should, therefore, be targeted by the solution above. For the sake of completeness, though, in case there were ever any browsers that supported that syntax without prefixing, you can include an additional check for the presence of the flex-order
property while also checking that the order
property isn't present in case there are any browsers that support both versions of the syntax.
var checkstyle=document.createElement("p").style;
if(!("flex" in checkstyle)||("flex-order" in checkstyle&&!("order" in checkstyle)))
document.getElementById("browserupgrade").style.display="block";
#browserupgrade{
display:none;
font-family:sans-serif;
}
<div id="browserupgrade">
<p>You are using an outdated browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
</div>
Firefox
Firefox added support for unprefixed flexbox properties in version 22 but did not support either the flex-flow
or flex-wrap
properties until version 28 so to add v22-27 to our targeting, we should instead check for the presence of one of those properties.
var checkstyle=document.createElement("p").style;
if(!("flex-flow" in checkstyle)||("flex-order" in checkstyle&&!("order" in checkstyle)))
document.getElementById("browserupgrade").style.display="block";
#browserupgrade{
display:none;
font-family:sans-serif;
}
<div id="browserupgrade">
<p>You are using an outdated browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
</div>
Internet Explorer 11
Despite not requiring prefixing, IE11's support for flexbox is still extremely buggy so you might want to consider targeting it as well. There is no way of doing so using any of the flexbox properties, however IE 7-11 (sort of) supported the image-rendering
property via the non-standard -ms-interpolation-mode
property, with support later dropped in Edge/v12+ so we can instead check for that. There is no guarantee, though, that this property won't be added back in to a future version of Edge so that should be monitored.
var checkstyle=document.createElement("p").style;
if(!("flex-flow" in checkstyle)||("flex-order" in checkstyle&&!("order" in checkstyle))||"-ms-interpolation-mode" in checkstyle)
document.getElementById("browserupgrade").style.display="block";
#browserupgrade{
display:none;
font-family:sans-serif;
}
<div id="browserupgrade">
<p>You are using an outdated browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
</div>
Opera Mini
Although caniuse.com claims full support for flexbox has existed in Opera Mini since version 5 and is widely considered to be an authoritative source for such information, if you still wish to target it, you can do so by checking for the presence of one of the myriad of properties it doesn't support. To keep things concise, though, I'd suggest replacing the -ms-interpolation-mode
check with a check for transform-style
which is not supported by Opera Mini nor by pre-Edge versions of IE. As with the previous check, this one should be monitored as future versions of Opera Mini which you might not want to target may continue not to support the transform-style
property.
var checkstyle=document.createElement("p").style;
if(!("flex-flow" in checkstyle)||("flex-order" in checkstyle&&!("order" in checkstyle))||!("transform-style" in checkstyle))
document.getElementById("browserupgrade").style.display="block";
#browserupgrade{
display:none;
font-family:sans-serif;
}
<div id="browserupgrade">
<p>You are using an outdated browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
</div>
UPDATES
13/05/16: Added additional checks to target more browsers after question was updated.
16/05/16: Added more detailed explanations of the proposed solution based on feedback provided in the comments and to make it worthy of the bounty on offer. Removed checks for old flexbox spec as they were redundant.
17/05/16: Added a temporary element to run the checks on, rather than using document.body
, following Gothdo's suggestion, which led to the removal of suggesting the use of getElementsByTagName()
for wider browser support.
23/05/16: Added notes with suggested checks for Internet Explorer 11 & Opera Mini.
You can use modernizr.js for feature detection.
And then write some JavaScript to re-direct the users to the URL above if the feature isn't found.
Yes we have a solution for this...
first of all download a custom build Modernizr from (click here --) https://modernizr.com/ (for flexbox property) then include latest Jquery to your page, add some stylesheet and you are done!
(Note: It is not necessary to download a custom build Modernizr, you can use whole complete build directly from Modernizr CDN but as you need to check Flexbox property only; thats why I told you to download a custom build)
Check this fiddle flexbox support fiddle
Your entire html page (with css, jquery ) will be like this...
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<title>BroswerCheck</title>
<style type="text/css">
#browserupgrade{
display:none;
text-align:center;
background:#F1070B;
color:#FFFFFF;
font-family:Segoe, "Segoe UI", Verdana, sans-serif;
font-size:15px;
padding:10px;}
#browserupgrade a{
color:#FFFFFF;
background:#000000;
text-decoration:none;
padding:5px 15px;
border-radius:25px;}
</style>
<script src="modernizr.js"></script>
</head>
<body>
<div id="browserupgrade">
<p>You are using an outdated browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
</div>
<script src="jquery-2.1.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
if (!Modernizr.flexbox) { $("#browserupgrade").show(); }
});
</script>
</body>
</html>
Your steps will be,
1. Download custom Modernizr to detect flexbox property
2. create a html page with class="no-js" added in html tag
3. create a div for displaying a message and hide it
4. add jquery and Modernizr to your page
5. show a div, when a browser doesn't support flexbox-property
(for that purpose use 'Modernizr.flexbox' property as shown in above script)
or if you dont want to use Jquery then use following script (thanks to @Shaggy) as,
$(document).ready(function () {
if(!Modernizr.flexbox)document.getElementByID("browserupgrade").style.display="block";
});
Okay, also I suggest to use modernizr
(download from https://modernizr.com/), but a little bit differently. I take it as a fact that your actual concern is only to detect if the browser supports flexbox or not, and not if it's a particular browser or browser version.
After you install it and load your page, modernizr will put classes into the <body>
tag. Concerning flexbox it will put either the class .flexbox
OR the class .no-flexbox
into the body tag, like <body class="no-flexbox bgsizecover csscalc">
(and in fact many more classes, unless you download a modernizr custom build only for flexbox). So you can use a simple combined selector for a CSS rule that selects a DIV block into which you write your warning concerning an outdated browser. Example:
Into the HTML, write something like:
<div class="browserwarning">Your browser is outdated and cannot display this page properly! Please install an up-to-date browser, which you can get from <a href="http://www.example.com">here</a>.</div>
In the CSS stylesheet, add this:
.browserwarning {
display: none;
}
.no-flexbox .browserwarning {
display: block;
font-size: 24px;
color: red;
background-color: yellow;
}
This will hide this message first (by the first rule), and then (second rule) only display it in browsers which cannot handle flexbox and therefore have the .no-flexbox
class put into the body
tag by modernizr when the page is loaded: The combined selector .no-flexbox .browserwarning
works regardless of where in the body that browser warning is placed - it doesn't have to be a direct child of body
, but can be anywhere in there.
This works for sure - I've used this in pages I did successfully...