Setting multiple cookies in Javascript

后端 未结 3 971
孤城傲影
孤城傲影 2021-02-07 03:11

I\'m trying to set multiple cookies in document.cookie, but unfortunately only one is getting added.

I know there are multiple examples present on the \'Net

3条回答
  •  旧巷少年郎
    2021-02-07 03:40

    Adding a cookie is performed via document.cookie = "name=value" to add multiple keys, you should perform multiple assigments

    function setCookie(start_time, end_session_time, total_time, flag, count) {
        document.cookie = "start_time=" + start_time;
    
        if (end_session_time) {
            document.cookie = "end_session_time=" + end_session_time;
        }
        if (total_time) {
            document.cookie = "total_time=" + total_time;
        }
        if (flag) {
            document.cookie = "flag=" + flag;
        }
        if (count) {
            document.cookie = "count=" + count;
        }
    
        console.log("document.cookie = " + document.cookie);
    }
    

提交回复
热议问题