Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save in local storage of Safari Private Mode

I have an app that saves a user name in local storage. It works fine with every browser except Safari in private mode.

Is there a way to save this variable in Safari private mode? I tried using cookies but it's also doesn't work...

Any work around?

like image 551
Offir Avatar asked Jan 07 '23 17:01

Offir


1 Answers

I implemented a LocalStorageHandler that checks if the browser supports local storage, if it doesn't support then I use a Cookie.

This is the function that checks if it supports local storage:

localStoreSupport: function ()
{
    var testKey = 'test', storage = window.sessionStorage;
    try
    {
        storage.setItem(testKey, '1');
        storage.removeItem(testKey);
        return true;
    }
    catch (error)
    {
        return false;
    }
}

And this is how I dealt with false:

if (this.localStoreSupport())
    {
        localStorage.setItem(name, value);
    }
    else
    {
        document.cookie = name + "=" + encodeURIComponent(value) + expires + "; path=/";
    }

I hope this helps you.

like image 179
Offir Avatar answered Jan 29 '23 10:01

Offir