Set cookie (with JS) for whole domain not specific page - javascript

I have a simple little script which I am using to set a cookie:
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
The problem I have this cookie is only set on one page, not across the whole domain.
How can I adjust this function so that the cookie remains across the whole domain?

You can specifiy domain ;domain=.example.com as well as path ;path=/ ("/" set cookie in whole domain)
document.cookie = cname + "=" + cvalue + "; " + expires +";path=/";

Related

Unable to set SameSite attribute on cookie using JavaScript in an iFrame

I have an issue setting a cookie with SameSite=none using JavaScript.
I have a client's site which pulls in content from our site into an iFrame. I need to be able to set a cookie on the user (of the client's site)'s machine which will remember a layout preference the next time the user visits the iFrame.
I have the following code which works fine in non-Chrome browsers:
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;
}
However, if I add in a line (highlighted below) to add the SameSite and Secure attributes, the cookie is not set (in any browser):
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());
**if (c_name == "preferenceCookie") { c_value += ";SameSite=None;Secure" };**
document.cookie = c_name + "=" + c_value;
}
I'm sure I'm missing something stupendously obvious, but this is driving me mad. (Or can it just not be done?)
(Oh - and it's my first post - please be gentle!)
Thanks.

How to load url along with saved Cookies in Javascript?

I would like to load my url using javascript, Where my url is required authorization token. The token already saved in cookies with the following code.
<script>
function setCookie(cname,cvalue,exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
</script>
And able to get saved token from cookies successfully but not able to apply for my url load which is required authentication token.
Can any one suggest me a way to load url with authorization token from cookies.
NOTE: I can not pass the authorization token in url as parameter.
Here is my url loading code:
<script>
function newDoc() {
window.location.assign("https://mydomine.com/Viewer?type=xyz")
}
</script>
If you are trying to set the cookie for a different domain, then you'll need to add the domain information to the cookie.
function setCookie(cname, cvalue, exdays, domain) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
var cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
// Add the domain, if specified.
if (domain) {
cookie += `;domain={domain}`
}
document.cookie = cookie;
}

Set Expiration on end of the day

I'm trying to set a cookie that will expire at the end of the day. I've create this function :
function mnc(cname,cvalue)
{
var now = new Date();
var expire = new Date();
expire.setFullYear(now.getFullYear());
expire.setMonth(now.getMonth());
expire.setDate(now.getDate()+1);
expire.setHours(0);
expire.setMinutes(0);
//alert(expire.toGMTString() + " " + expire.toString());
var expires = "expires="+expire.toString();
alert(expires + "=> now =" + now);
document.cookie = cname + "=" + cvalue + "; " + expires +"; path=/";
}
On Fiddle : http://jsfiddle.net/MYs6b/
So, the alert box show me the good expiration date.
But, if I change the date on my computer by adding 1 or 100 days, i still have the same value in the cookie.
Why? I'm searching since 3 hours and i don't understand...
EDIT :
I've had an alert on "document.cookie" is empty
http://jsfiddle.net/MYs6b/2/
EDIT 2 :
I've add a better example of my problem. It's working on IE and FF but not on chrome :
http://jsfiddle.net/5h87M/1/
Try This: http://jsfiddle.net/MYs6b/1/
function mnc(cname,cvalue)
{
var now = new Date();
var expire = new Date();
expire.setFullYear(now.getFullYear());
expire.setMonth(now.getMonth());
expire.setDate(now.getDate()+1);
expire.setHours(0);
expire.setMinutes(0);
expire.setSeconds(0);
var expires = "expires="+expire.toString();
alert(expires + "=> now =" + now);
document.cookie = cname + "=" + cvalue + "; " + expires +"; path=/";
}
mnc("test", "123456");
Last update of chrome correcting this issue.
function createCookie(name,value,path) {
var expires = "";
var date = new Date();
var midnight = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59);
expires = "; expires=" + midnight.toGMTString();
if (!path) {
path = "/";
}
document.cookie = name + "=" + value + expires + "; path=" + path;
}

avoid cookies to be sent to sub domain

How to avoid cookies to be sent to sub domain
Using below script
function createCookie(name,value,days){
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
Can you not just check the domain thats calling the script first and then only set the cookie if it's not a sub domain?
Something like:
if (Request.Host.toLower() == "www.abc.com"){
createCookie(name,day,values);
}
Below script worked for me
document.cookie = cookieName +"=" + cookieValue + ";expires="
+ myDate + ";domain=www.abc.com;path=/";

I don't want a specific path for my cookie using javascript

I am setting a cookie, but I don't want it to just work for one directory. How do I make it to where it's read throughout my whole site? I'm pretty new at JavaScript, thanks guys!
<script>
function setCookie(val) {
var d = new Date();
d.setDate(d.getDate() + 300);
document.cookie = "roster_count" + "=" + escape(val) + "; expires=" + d.toGMTString();
delete d;
}
</script>
The the path /:
document.cookie = "roster_count" + "=" + escape(val) + "; expires=" + d.toGMTString() + "; path=/";

Categories