W3C home > Mailing lists > Public > public-webapps@w3.org > July to September 2009

Re: Web Storage how to check for support?

From: Darin Adler <darin@apple.com>
Date: Wed, 05 Aug 2009 22:00:03 -0700
Cc: WebApps WG <public-webapps@w3.org>
Message-id: <45DF3D51-A5E9-4F05-A8F5-072AD6F483C5@apple.com>
To: (wrong string) ありがとうございました" <j.chetwynd@btinternet.com>
On Aug 5, 2009, at 9:42 PM, Jonas Sicking wrote:

> 2009/8/1 Joo Eiras <joaoe@opera.com>:
>>> How can one in a script check for UA support?
>>> else if(localStorage){}
>>> does not work for Opera**.
>> This obviously does not work because you need to prefix  
>> localStorage with window "if(window.localStorage)" else the script  
>> breaks because localStorage is a undefined identifier while  
>> window.localStorage looks up a property in the window. Different  
>> things, specific to ecmascript itself.
> No, the window object is the global object where all global  
> variables are defined. Try the following:
> <script>
> a = 10;
> alert(window.a);
> </script>

I think the point is that if there is no global variable of name "x",  
then "window.x" yields undefined, whereas "x" yields an exception.

That means that one way to check for the localStorage object is:

     if (window.localStorage) {
         // Code using localStorage here.

If you instead do:

     if (localStorage) {
         // Code using localStorage here.

Then you end up with code that simply throws an exception in browsers  
that dont have localStorage.

     -- Darin
Received on Thursday, 6 August 2009 05:00:45 UTC

This archive was generated by hypermail 2.3.1 : Friday, 27 October 2017 07:26:18 UTC