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

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.

Related

How to set cookie for a day

This is the code I'm using. Please help me to set cookie for a day! I want to show welcome page when a visitor come back after 24 hours.
<SCRIPT LANGUAGE="JavaScript">
<!-- hide from old browsers
function setCookie(name, value, expires, path, domain, secure) {
document.cookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
function setsplash() {
setCookie("splash", "1", "", "/");
}
//-->
</SCRIPT>
Your method setCookie() is defined as
function setCookie(name, value, expires, path, domain, secure)
The third parameter is the expiration time. But you're calling
setCookie("splash", "1", "", "/");
i.e. the expires parameter is "". If you want the cookie to expire after 24 hours, use something like this instead:
var expires = (new Date(Date.now() + 86400000)).toUTCString(); // 86400000 milliseconds is 24 hours
setCookie("splash", "1", expires, "/");

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

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=/";

How to set Javascript cookie in all the pages / globally?

I'm having a huge problem with my code, I am trying to check if a cookie exists, and if it does exist, i dont want it to do anything.
Its working fine on page1, but when i navigate to page2 it overrides the cookie, instead of doing nothing (the pages is from the same website)
Heres my script
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);
}
}
}
var omgpost = getCookie("omgpost");
if (omgpost == null || omgpost == "") {
setCookie("omgpost", "1", 1);
} else {
alert('cookie installed already');
}
This is working fine, when I don't have the cookie installed and entering this site, I'm adding the cookie, and I'm getting the confirmation message each time I refresh the page1.
But when navigating to page2 its recreating the cookie??? I don't want that! I want the cookie to be there, and can't be changed, only when it has expired, how can I do that?
Set Cookie Path using path=/
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())
+ "; path=/";
document.cookie=c_name + "=" + c_value;
}

Javascript: Cookie can not be read in all the HTML pages

I copied and used getCookie and setCookie from W3Schools(http://www.w3schools.com/js/js_cookies.asp). Here are the codes of get and set:
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 c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
I frist set cookie in prepareDrive.html page
setCookie("pathName",path,365);
setCookie("formatName",ifFormat,365);
Then I called get cookie in startInstall.html page which is a different HTML page
var path = getCookie("pathName");
var ifFormat = getCookie("formatName");
but both path and ifFormat are null. However, when I console.log in prepareDrive.html, the data is there. Thanks !!! This is my first time to use cookie in JS. I do not want to use localstorage to store data.Because some old version browsers do not support this function ,right?
You need to specify a common path for the cookie. Simplest is just to specify the domain root:
var c_value=escape(value) + "; path=/" + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
Without that, document.cookie will default to the current location.pathname, making the cookie only available to the current page.
;path=path (e.g., '/', '/mydir') If not specified, defaults to the current path of the current document location.
Also, I suggest having a look at the "little framework" for cookies on MDN.
If you are already using jquery you might want to consider the jquery plugin:
https://github.com/jquery/plugins.jquery.com
That makes it pretty straightforward.

Is there a way I can have Javascript tell me why a cookie wasn't set?

Here is my code for setting a cookie:
function setCookie(c_name, value, expiredays)
{
alert("setting cookie!");
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie = c_name + "=" + escape(value) +
((expiredays === null) ? "" : ";expires=" + exdate.toUTCString());
}
I'm using Phonegap to make an app on Android phones, which should just use and set cookies like a regular browser. It doesn't seem to be setting the cookie, though, and because of the environment (on my pnone), I can't debug.
So, is there a way I can make an alert to tell me if document.cookie fails, and ideally any error message that might tell me why?
I tried just doing alert(document.cookie), but it's not outputting anything, so I'm hoping there's another way to trap the output.
You can use try/catch statement:
function setCookie(c_name, value, expiredays)
{
try {
alert("setting cookie!");
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie = c_name + "=" + escape(value) +
((expiredays === null) ? "" : ";expires=" + exdate.toUTCString());
} catch(error) {
// deal with errors
alert(error);
}
}

Categories