How to get session cookie via javascript?

6

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?

    
asked by anonymous 06.06.2016 / 15:28

4 answers

5

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

    
06.06.2016 / 15:47
2

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;
    
06.06.2016 / 15:43
2

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.

    
06.06.2016 / 15:48
2

Use the

link

Create

$.cookie("exemplo", "abc");
$.cookie("exemplo", "abc", { expires: 7 }); 
$.cookie("exemplo", "abc", { path: '/admin', expires: 7 });

Read

alert( $.cookie("exemplo") );

Remove

$.removeCookie("exemplo");

Source: link

    
06.06.2016 / 15:53