I need to make a script that when the user arrives in my utm via url, I should write a session cookie. This cookie will expire when the browser closes. Can someone help me with this?
I need to make a script that when the user arrives in my utm via url, I should write a session cookie. This cookie will expire when the browser closes. Can someone help me with this?
To create a session cookie, simply associate a string containing the chave=valor
format with the cookie
property of document
:
document.cookie = "[nomeDoCookie]=[ValorDoCookie]";
If you want to persist this cookie between sessions of the browser, you can specify an expiration date in format GMTString :
document.cookie = "[nomeDoCookie]=[ValorDoCookie]; expires=[data]";
Examples:
document.cookie = "cookie2=sample";
document.cookie = "cookie2=true; expires=Fri, 31 Dec 9999 23:59:59 GMT";
However it is worth mentioning that there is no guarantee that the session cookie will be waived when all windows are closed - Chrome, for example, has a setting that by default keeps cookies from session live:
Sources:
MDN- Document.cookie
StackOverflow - Chrome does not delete session cookies
To create the cookie:
document.cookie = "username=[utilizador]; expires=[dia que expira], [18 Dec 2013 12:00:00 UTC]";
To read the cookie:
var x = document.cookie;
Frames,
According to wikipedia , to do what you want a session cookie should be created, which is nothing else is a cookie with no expiration date.
When you create the cookie with no expiration date, it is only in memory and as soon as the browser is closed the cookie is removed.
The session cookie, also known as an in-memory cookie or transient cookie, exists only in temporary memory while the user navigates the website. [12] Web browsers normally delete session cookies when the user closes the browser. [13] Unlike other cookies, session cookies do not have an expiration date assigned to them, which is how the browser knows to treat them as session cookies.