I have some JS using ES6 template literals that I want to ensure has a fallback for older browsers.
Normally to detect a javascript feature I would do the standard if statement to see if that feature is detected in the window object:
if("JavascriptFeature" in window){
// do something with the feature
}
How would I do this with template literals in the context below though?
I have the code below which is basically used to ensure that a 100vh property works as desired on mobile / iPad, and I want to wrap the JS into a conditional that only fires if the browser can use template literals:
JS
function resizeSection(){
// First we get the viewport height and we multiple it by 1% to get a value for a vh unit
var vh = window.innerHeight * 0.01;
// Then we set the value in the --vh custom property to the root of the document
document.documentElement.style.setProperty('--vh', `${vh}px`);
}
resizeSection();
addEventListener("resize", resizeSection, false)
CSS
.heading-section {
/* Use vh as a fallback for browsers that do not support Custom Properties */
height: calc(100vh);
/*MODERN BROWSERS*/
height: calc(var(--vh, 1vh) * 100);
}
Please note: this is not a duplicate of Detecting template literals in javascript which is a similar sounding question in a very different context.