V
V
Vlad_isLove2019-07-26 18:23:27
PHP
Vlad_isLove, 2019-07-26 18:23:27

If you save a cookie in the browser using JS, will the cookie be passed to the server?

There is a JavaScript variable that is responsible for the height of the block on the page, I want to save this variable and set the desired height when the page is reloaded. Cookies are stored using this code:

function setCookie(name, value, options={}) {
    var expires = options.expires;
    if (typeof expires == "number" && expires) {
        var d = new Date();
        d.setTime(d.getTime() + expires * 1000);
        expires = options.expires = d;
    }
    if (expires && expires.toUTCString) {
        options.expires = expires.toUTCString();
    }
    value = encodeURIComponent(value);
    var updatedCookie = name + "=" + value;
    for (var propName in options) {
    updatedCookie += "; " + propName;
    var propValue = options[propName];
    if (propValue !== true) {
        updatedCookie += "=" + propValue;
    }
    }
document.cookie = updatedCookie;
}

This will only save the cookie in the browser without interacting with the server?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2019-07-26
@Vlad_isLove

the created cookie will be sent to the server on the next request.

L
Lynn "Coffee Man", 2019-07-26
@Lynn

Cookies are always sent to the server

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question