Game Development Reference
In-Depth Information
Gracefully degrade
If you pay close attention to the previous code snippet where we attempted to create
an XHR object that works in many different browsers, you will notice that the code
deliberately halts execution if the browser executing the code doesn't support one of
the two options the code searched for. That is a great example of what we should not
do, if at all possible. Whenever a specific feature is not available to a certain browser,
the first option should be to provide an alternative construct, even if this alternative
method doesn't quite provide the same behavior. We should do all that we can to at
least provide a functional experience in the worst case scenario, where the browser
has zero support for what we're trying to accomplish.
For example, HTML5 provides a new storage mechanism that's similar to a cookie
(in other words, a simple key-value pair storage), but with the main difference being
that this storage mechanism stores the data completely in the client, and that data is
never sent back and forth to the server as part of the HTTP request. While the specif-
ics of what this storage system is and how it works will be covered later in the topic,
we can summarize it by saying that this storage system (called Local Storage) stores
key-value pairs, and does so through a well defined interface, and from a property of
the Window object named localStorage .
localStorage.setItem("name", "Rodrigo
Silveira");
localStorage.length == 1; // true
localStorage.getItem("name"); // "Rodrigo
Silveira"
localStorage.removeItem("name");
localStorage.length; // == 0
One powerful application for Local Storage is to cache asynchronous requests made
by the user, so that the subsequent requests can be fetched directly from the
browser's local storage, thus avoiding the round trip to the server. However, if a
browser doesn't support local storage, the worst case scenario in this particular case
would be that the application would need to fetch a subsequent request from the
server again. While not practical or efficient, this is by far a problem one should not
lose sleep over, except if that means that we'll need to write lots of extra code to test
for the presence of the localStorage object every time we need to use it, thus
polluting the code base with many repetitive conditional statements.
Search WWH ::




Custom Search