How to save in local storage of Safari Private Mode
Asked Answered
F

2

8

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?

Forras answered 19/8, 2015 at 16:34 Comment(2)
Did you find a way to deal with this? I've tried using amplify.js which wraps multiple storage technologies. But after a window.location the memory is cleared again.Venous
@pidm , I answered the question.Forras
F
8

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.

Forras answered 3/12, 2015 at 9:11 Comment(1)
Hello Offir, I suggested an edit with changing testKey from test to __localStoreTest__ which got a few approves before you rejected it. I believe that they key used here ('test') is too normal to use in such a check, it might very well create a nightmare for someone who just copied code from here and later started using localStorage with 'test' key somewhere else. Could you, please, explain why you rejected it?Pectinate
P
6

From Safari 11 onwards they support localStorage in private mode, the same as other browsers Commit: https://github.com/WebKit/WebKit/commit/91d15f887ff7174f7754b25b8dc8ab459951e5e1 Discussion: https://bugs.webkit.org/show_bug.cgi?id=157010

Polymath answered 19/11, 2021 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.