Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]--> 
like image 230
Michael Benjamin Avatar asked Oct 27 '15 02:10

Michael Benjamin


2 Answers

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); } 
like image 96
Asons Avatar answered Sep 23 '22 02:09

Asons


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.

like image 44
Shaggy Avatar answered Sep 20 '22 02:09

Shaggy