使用 Javascript 仅会话 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14196671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:58:42  来源:igfitidea点击:

Session only cookies with Javascript

javascripthtml

提问by Daan Poron

I was wondering if it's possible to create session only cookies with Javascript. When the browser is closed the cookies should be removed.

我想知道是否可以使用 Javascript 创建仅会话 cookie。当浏览器关闭时,cookie 应该被删除。

I can't use anything on the server as the website is HTML only ... so no server side script is used.

我不能在服务器上使用任何东西,因为网站只是 HTML ......所以没有使用服务器端脚本。

I read something about this here: http://blog.lysender.com/2011/08/setting-session-only-cookie-via-javascript/but i can't find any more information about this ... so i was wondering if this method is reliable.

我在这里读到了一些关于这个的东西:http: //blog.lysender.com/2011/08/setting-session-only-cookie-via-javascript/但我找不到关于这个的更多信息......所以我是想知道这个方法是否可靠。

回答by Rhumborl

Yes, that is correct.

对,那是正确的。

Not putting an expirespart in will create a session cookie, whether it is created in JavaScript or on the server.

不放入expires部件将创建会话 cookie,无论它是在 JavaScript 中创建的还是在服务器上创建的。

See https://stackoverflow.com/a/532660/1901857

https://stackoverflow.com/a/532660/1901857

回答by Cerbrus

A simpler solution would be to use sessionStorage, in this case:

一个更简单的解决方案是使用sessionStorage,在这种情况下:

var myVariable = "Hello World";

sessionStorage['myvariable'] = myVariable;

var readValue = sessionStorage['myvariable'];
console.log(readValue);

However, keep in mind that sessionStoragesaves everything as a string, so when working with arrays / objects, you can use JSON to store them:

但是,请记住,sessionStorage将所有内容保存为字符串,因此在处理数组/对象时,您可以使用 JSON 来存储它们:

var myVariable = {a:[1,2,3,4], b:"some text"};

sessionStorage['myvariable'] = JSON.stringify(myVariable);
var readValue = JSON.parse(sessionStorage['myvariable']);

A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated.

只要浏览器打开,页面会话就会持续,并在页面重新加载和恢复后继续存在。在新选项卡或窗口中打开页面将导致启动新会话。

So, when you close the page / tab, the data is lost.

因此,当您关闭页面/选项卡时,数据将丢失。

回答by Gaurav Agarwal

For creating session only cookie with java script, you can use the following. This works for me.

要使用 java 脚本创建仅会话 cookie,您可以使用以下内容。这对我有用。

document.cookie = "cookiename=value; expires=0; path=/";

then get cookie value as following

然后获取cookie值如下

 //get cookie 
var cookiename = getCookie("cookiename");
if (cookiename == "value") {
    //write your script
}

//function getCookie        
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1);
        if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
    }
    return "";
}

Okay to support IE we can leave "expires" completely and can use this

好的,为了支持 IE,我们可以完全离开“过期”并可以使用它

document.cookie = "mtracker=somevalue; path=/";