Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE8 and localStorage support [duplicate]

I am not sure if IE8 fully supports localStorage. But I use the following method to detect

function supports_html5_storage() 
{
    try { 
    return 'localStorage' in window && window['localStorage'] !== null; 
    } 
    catch (e) {
    return false; 
    } 
}

Now IE returns true for 'localStorage' in window

But returns undefined for window['localStorage']

So should I update this method OR does IE8 indeed have local storage support ?

like image 350
Test Test Avatar asked Dec 12 '22 17:12

Test Test


1 Answers

You can try to set and read localStorage.

Some browsers return a security error if cookies are disabled or you are working with file: protocol.

function hasStorage(){
    try{
        localStorage.setItem('test', '7');
        if(localStorage.getItem('test')=== '7'){
            localStorage.removeItem('test');
            return true;
        }
    }
    catch(er){}
    return false;
}

alert(hasStorage())

like image 166
kennebec Avatar answered Jan 01 '23 23:01

kennebec