if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition((position) => {
// show the location on a map, such as the Google Maps API
});
} else {
// Give the user a choice of static maps
}Implementing feature detection / notebook
Implementing feature detection
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
<link href="basic-styling.css" rel="stylesheet" />
<link class="conditional" href="grid-layout.css" rel="stylesheet" />const conditional = document.querySelector(".conditional");
if (CSS.supports("grid-template-columns", "subgrid")) {
conditional.setAttribute("href", "subgrid-layout.css");
}@supports (grid-template-columns: subgrid) {
main {
display: grid;
grid-template-columns: repeat(9, 1fr);
grid-template-rows: repeat(4, minmax(100px, auto));
}
.item {
display: grid;
grid-column: 2 / 7;
grid-row: 2 / 4;
grid-template-columns: subgrid;
grid-template-rows: repeat(3, 80px);
}
.subitem {
grid-column: 3 / 6;
grid-row: 1 / 3;
}
}@supports not (grid-template-columns: subgrid) {
/* rules in here */
} if ("geolocation" in navigator) {
// Access navigator.geolocation APIs
} function supportsCanvas() {
return !!document.createElement("canvas").getContext;
}
if (supportsCanvas()) {
// Create and draw on canvas elements
}if (window.matchMedia("(width <= 480px)").matches) {
// run JavaScript in here.
}<link href="dist/brick.css" rel="stylesheet" media="(width <= 480px)" />if (window.matchMedia("(width <= 480px)").matches) {
deck.shuffleTo(1);
}