diff --git a/static/units.js b/static/units.js index c4b0187..14f12ba 100644 --- a/static/units.js +++ b/static/units.js @@ -20,8 +20,20 @@ function defaultUnit() { } return "F"; // no region subtag to go on → keep the historical default } +// A climate page knows which city it is about, so it publishes that country's +// convention on . It sits *between* the stored choice and +// the locale guess: better than the locale (a UK visitor reading about Phoenix +// wants the °F the page already rendered, and repainting it to °C would just be a +// flash), but never better than an explicit toggle. Pages with no single city — +// the homepage, the interactive views — emit no attribute and fall through. +function pageDefault() { + const v = document.body && document.body.dataset.unitDefault; + return (v === "C" || v === "F") ? v : null; +} const _stored = localStorage.getItem(UNIT_KEY); -let _unit = (_stored === "C" || _stored === "F") ? _stored : defaultUnit(); +let _unit = (_stored === "C" || _stored === "F") + ? _stored + : (pageDefault() || defaultUnit()); const _unitCbs = []; export function getUnit() { return _unit; }