I am working on a project in which I have a responsive grid which I have achieved using flex wrap property. Since I am supporting IE9 and lower versions of Firefox, version 28 and below, how do I find out, through javascript, the support for the same. Currently I have only been able to identify IE9 browser through conditional statement but does anyone now how to detect for Firefox older versions through javascript.
Flexbox is very well supported across modern browsers, however there are a few issues that you might run into. In this guide we will look at how well flexbox is supported in browsers, and look at some potential issues, resources and methods for creating workarounds and fallbacks.
The basic idea is to loop through the flex items and test their top position against the previous sibling. If the top value is greater (hence further down the page) then the item has wrapped. The function detectWrap returns an array of DOM elements that have wrapped, and could be used to style as desired.
Safari versions 9 and up support the current flexbox spec without prefixes. Older Safari versions, however, require -webkit- prefixes. Sometimes min-width and max-width cause alignment problems which can be resolved with flex equivalents. See Flex items not stacking properly in Safari.
The Flexbox Inspector allows you to examine CSS Flexbox Layouts using the Firefox DevTools, which is useful for discovering flex containers on a page, examining and modifying them, debugging layout issues, and more.
I found that this is the simplest method:
var d = document.documentElement.style
if (('flexWrap' in d) || ('WebkitFlexWrap' in d) || ('msFlexWrap' in d)){
alert('ok');
}
I tried hasOwnProperty
too but it doesn't work in IE and FF.
So why use 40 KB of modernizr when you can have just 3 lines of js ?
A simple CSS property detection technique is to test it directly on an element and check if it returns undefined
like below,
element.style.<propertyName> != undefined
.
The above method directly checks for the property and should be sufficient for most common properties (not for flex-wrap).
function isStyleSupported(el, property) {
return el.style[property] != undefined;
}
var testEl = document.getElementById('test');
testEl.innerHTML = (isStyleSupported(testEl, 'flexWrap')) ? "Flex Wrap is supported" : "Flex Wrap is NOT supported";
<div id="test"></div>
You can add a little more code to check if the property is supported with dom prefixes,
var domPrefixes = 'Webkit Moz O ms Khtml'.split(' ');
function toCamelCase(cssProp) {
return cssProp.replace(/-([a-z])/gi, function(s, prop) {
return prop.toUpperCase();
});
}
function isStyleSupported(el, property) {
if (el.style[toCamelCase(property)] != undefined) {
return true;
} //supported
property = toCamelCase("-" + property);
for (var i = 0; i < domPrefixes.length; i++) { //check with dom prefixes
if (el.style[domPrefixes[i] + property] !== undefined) {
return true; //supported with dom prefix
}
}
}
var divEl = document.getElementById('result'), textEl = document.getElementById('textbox');
document.getElementById('checkSupport').onclick = function() {
divEl.innerHTML = (isStyleSupported(divEl, textEl.value)) ? textEl.value + " is supported" : textEl.value + " is NOT supported";
};
<input type="text" id="textbox" value="flex-wrap" />
<button id="checkSupport">Check</button>
<div id="result"></div>
It really gets complicated when you decide to generalize this for any property across all browser. This is where we decide to go for modernizr
which has extended support to handle exceptions cases.
There is a new CSS API CSS.supports
which returns a boolean to check if a specific CSS feature is supported by the browser. However this is a new API so we still be using plugins like modernizr
until there is a support required for older browser.
Use the simplest style detection element.style.<property> != undefined
or with domPrefixes
if your requirement is simple. You can always customize it a little more as your need, but if it is complicated and extensive, go for modernizr
which has extended code for feature detection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With