LocalStorage and Safari Weirdness

One of the technologies that has been intriguing to me for a while has been LocalStorage on the web browser. One of my first adventures into using persistent storage other than cookies on a web browser was the short-lived HTML 5 standard of the webSQL database. It turns out that it was simply a SQL Lite database that was accessible via JavaScript in all the WebKit browsers as well as Firefox. However, Internet Explorer did not implement this functionality, and the webSQL standard was soon dropped from the HTML 5 standard itself, leaving only the LocalStorage key/value storage mechanism.

With the advent of the now hugely popular NoSQL databases such as MongoDB as well as Redis and others, the power of a key/value store in the browser has become immensely evident. In addition, with LocalStorage, you can be assured that every major browser supports it, even Internet Explorer 8 and newer. As a result, you would take a look at the W3Schools site on LocalStorage and assume that you can do something simple like:

if (typeof Storage !== "undefined") {
    localStorage.setItem("key", "value");
}

While this will work in all browsers, there are some scenarios that render this check useless. The most important of all to consider is Private Browsing in Safari across all devices and operating systems. It turns out that when you enable Private Browsing in Safari, the Storage object is still defined, but when you go to actually use localStorage it will throw an exception that will completely derail the rest of the JavaScript on your website. In order to be absolutely sure whether you can use localStorage on a particular browser, you should use something similar to the following to detect its availabiliy:

var useLocalStorage = false;

try {
    if (typeof Storage !== "undefined") {
        useLocalStorage = true;
        localStorage.setItem("key", "value");
    }
} catch (e) {
    useLocalStorage = false;
}

if (useLocalStorage) {
	// Do your localStorage operations here
}
comments powered by Disqus

Related Posts

Always Use Automated Integration Testing

QA or Quality Assurance of a software project is often the area of software development that is most neglected. Typically developers avoid software testing like their lives depended on it. While a basic level of testing is required for a single scenario to validate that your code “works”, the level of testing that is required to ensure that all users have a good user experience across all targeted platforms is something that a developer seems to think is beneath them.

Read More

JavaScript Templating

Many times it becomes useful to be able to make an AJAX request for some data, insert it into some HTML that is already on the client, and then display it to the user. There are a few ways to implement this, each approach has its benefits and drawbacks. String Concatenation Possibly the simplest way to accomplish the templating in JavaScript is to use simple string concatenation with ‘+’. This is the approach that I see many newcomers to JavaScript use in their code, as it is the simplest to implement.

Read More

Parallax Background Scrolling on Internet Explorer is Not Smooth

One of the pleasures of working on a website that is using some of the latest technologies is that you often run into strange compatability issues that only affect one browser or another, and many of the forums have little to no information about how to properly address the issues. Parallax scrolling is a technique that has been around for a while now, highlighted by Apple’s own iPhone 5s card-esque scrolling on their homepage, among others.

Read More