diff --git a/js/Map.js b/js/Map.js index 353d000..50462f1 100644 --- a/js/Map.js +++ b/js/Map.js @@ -60,7 +60,7 @@ BR.Map = { map = new L.Map('map', { worldCopyJump: true }); - if (!map.restoreView()) { + if (!BR.Util.localStorageAvailable() || !map.restoreView()) { map.setView([50.99, 9.86], 6); } map.attributionControl.addAttribution( diff --git a/js/Util.js b/js/Util.js index 993dc5c..4ff5d8a 100644 --- a/js/Util.js +++ b/js/Util.js @@ -29,6 +29,26 @@ BR.Util = { msg = xhr.status + ': ' + xhr.statusText; } return new Error(msg); - } + }, + // check if localStorage is available, especially for catching SecurityError + // when cookie settings are blocking access (Chrome, Pale Moon, older Firefox) + // + // see also https://github.com/Modernizr/Modernizr/blob/master/feature-detects/storage/localstorage.js + // + // https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#Testing_for_support_vs_availability + // by Mozilla Contributors, with modifications; + // Any copyright is dedicated to the Public Domain. http://creativecommons.org/publicdomain/zero/1.0/ + localStorageAvailable: function() { + try { + var storage = window.localStorage, + x = '__storage_test__'; + storage.setItem(x, x); + storage.removeItem(x); + return true; + } + catch(e) { + return false; + } + } }; \ No newline at end of file diff --git a/js/control/OpacitySlider.js b/js/control/OpacitySlider.js index 847c961..8234806 100644 --- a/js/control/OpacitySlider.js +++ b/js/control/OpacitySlider.js @@ -7,7 +7,7 @@ BR.OpacitySlider = L.Control.extend({ onAdd: function (map) { var container = L.DomUtil.create('div', 'leaflet-bar control-slider'), input = $(''), - item = localStorage.opacitySliderValue, + item = BR.Util.localStorageAvailable() ? localStorage.opacitySliderValue : null, value = item ? parseInt(item) : BR.conf.defaultOpacity * 100, minOpacity = (BR.conf.minOpacity || 0) * 100; @@ -39,7 +39,9 @@ BR.OpacitySlider = L.Control.extend({ }).on('slide slideStop', { self: this }, function (evt) { evt.data.self.options.callback(evt.value / 100); }).on('slideStop', function (evt) { - localStorage.opacitySliderValue = evt.value; + if (BR.Util.localStorageAvailable()) { + localStorage.opacitySliderValue = evt.value; + } // When dragging outside slider and over map, click event after mouseup // adds marker when active on Chromium. So disable click (not needed)