Last active
June 29, 2024 13:49
-
-
Save davidhund/b995353fdf9ce387b8a2 to your computer and use it in GitHub Desktop.
The simplest feature-detect for flexbox?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Trying to feature-detect (very naive) | |
* CSS Flexbox support. | |
* - Only most modern syntax | |
* | |
* Is this nonsense? | |
*/ | |
(function NaiveFlexBoxSupport(d){ | |
var f = "flex", e = d.createElement('b'); | |
e.style.display = f; | |
return e.style.display == f; | |
})(document); |
Or you could make it css property generic
function cssPropertySupported(pNames) {
const element = document.createElement('a')
let index = pNames.length
try {
while (index--) {
const name = pNames[index]
element.style.display = name
if (element.style.display === name) {
return true
}
}
} catch (pError) {}
return false
}
cssPropertySupported(['-webkit-box', '-ms-flex', 'flex'])
according to mcshaman post:
cssPropertySupported(['-webkit-box', '-ms-flex', 'flex'])
should be:
cssPropertySupported(['-ms-flexbox', '-webkit-box', 'flex'])
By the way:
return true
should be:
return name
so you know what syntax is used
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wrote a version of Davids script that returns a boolean