Re: Web Storage how to check for support?

2009-08-06 Thread Garrett Smith
2009/8/5 Darin Adler da...@apple.com:
 On Aug 5, 2009, at 9:42 PM, Jonas Sicking wrote:

 2009/8/1 João Eiras jo...@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


There is no variable declaration there.

 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
 don’t have localStorage.


Correct.

The 'typeof' operator would be another good option here.

Type converting and even getting host object properties has been the
source of errors in IE.

For example:-

javascript: var op = document.createElement(div).offsetParent;

- errs in IE.

The typeof operator can be used to avoid such errors.

if(typeof window.localStorage != undefined){
 // property is not undefined.
}

Considerations of usage context are important and relevant. Good question.

    -- Darin




Regards,

Garrett



Re: Web Storage how to check for support?

2009-08-05 Thread Darin Adler

On Aug 5, 2009, at 9:42 PM, Jonas Sicking wrote:


2009/8/1 João Eiras jo...@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 don’t have localStorage.


-- Darin




Re: Web Storage how to check for support?

2009-08-05 Thread Jonas Sicking
2009/8/1 João Eiras jo...@opera.com:


 How can one in a script check for UA support?
 else if(localStorage){}
 does not work for Opera**.

 Hi.
 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

/ Jonas



Re: Web Storage how to check for support?

2009-08-01 Thread João Eiras



How can one in a script check for UA support?
else if(localStorage){}
does not work for Opera**.


Hi.
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.





Re: Web Storage how to check for support?

2009-07-27 Thread Joseph Pecoraro

http://dev.w3.org/html5/webstorage/#the-storage-interface
Web Storage how to check for support?

How can one in a script check for UA support?


How about either of the following, they both worked as expected for me.

  // Check for the Storage interface
  if (window.Storage) { ... }

  // Check for an instance of the interface
  if (window.localStorage) { ... }

- Joe



Re: Web Storage how to check for support?

2009-07-27 Thread ~:'' ありがとうございました

Joseph,

thanks for that,

had tried document but not window

regards

~:

On 27 Jul 2009, at 16:41, Joseph Pecoraro wrote:


http://dev.w3.org/html5/webstorage/#the-storage-interface
Web Storage how to check for support?

How can one in a script check for UA support?


How about either of the following, they both worked as expected for  
me.


 // Check for the Storage interface
 if (window.Storage) { ... }

 // Check for an instance of the interface
 if (window.localStorage) { ... }

- Joe