Save Cookies, Then Open Link in New Tab
Posted
by
speedplane
on Stack Overflow
See other posts from Stack Overflow
or by speedplane
Published on 2012-03-23T04:52:52Z
Indexed on
2012/03/23
5:30 UTC
Read the original article
Hit count: 111
JavaScript
|cookies
I have some javascript code that saves a cookie. However, if after saving the cookie, the user opens a new tab, it appears that the cookie is not saved. The new tab is on the same domain.
Here is my cookie setting/getting code:
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++) {
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name) {
return unescape(y);
}
}
}
If some javascript calls setCookie('mycookie', 1)
and then the user clicks on a link where the _target
is set to _blank
, the cookie does not load in the new tab. So getCookie('mycookie')
will not return 1
.
What is the problem here?
© Stack Overflow or respective owner