how to clear localstorage,sessionStorage and cookies in javascript? and then retrieve?

前端 未结 3 1528
旧时难觅i
旧时难觅i 2020-12-05 04:07

How to completely clear localstorage, sessionStorage and cookies in javascript ?

Is there any way one can get these values bac

3条回答
  •  春和景丽
    2020-12-05 04:24

    how to completely clear localstorage

    localStorage.clear();
    

    how to completely clear sessionstorage

    sessionStorage.clear();
    

    [...] Cookies ?

    var cookies = document.cookie;
    
    for (var i = 0; i < cookies.split(";").length; ++i)
    {
        var myCookie = cookies[i];
        var pos = myCookie.indexOf("=");
        var name = pos > -1 ? myCookie.substr(0, pos) : myCookie;
        document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
    }
    

    is there any way to get the value back after clear these ?

    No, there isn't. But you shouldn't rely on this if this is related to a security question.

提交回复
热议问题