Cannot set cookies in Javascript Cannot set cookies in Javascript google-chrome google-chrome

Cannot set cookies in Javascript


Recently I stumbled upon a similar issue. My script couldn't store a cookie in Chromium, although it worked well on all other major browsers. After some googling it turned out that Chrome ignores cookies from local pages. After I uploaded the page to remote server code magically started to work.


I tried to save an cookie on Chrome and had the same error, that it would save.Although it did work in Firefox, so I asked an collegue and he suggested that I take away path and domain (I had name of cookie, value, expire, path amd domain) and all of a sudden it worked.So just to get the cookie to actually save in Chrome, you just need name, value and expire.

Hope it helps.

Example:

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+";";}


Chrome denies file cookies. To make your program work, you going to have to try it in a different browser or upload it to a remote server. Plus, the code for your setcookie and getcookie is essentially wrong. Try using this to set your cookie:

function setCookie(name,value,expires){   document.cookie = name + "=" + value + ((expires==null) ? "" : ";expires=" + expires.toGMTString())}

example of usage:

var expirydate=new Date();expirydate.setTime( expirydate.getTime()+(100*60*60*24*100) )setCookie('cookiename','cookiedata',expirydate)// expirydate being a variable with the expiry date in it// the one i have set for your convenience expires in 10 days

and this to get your cookie:

function getCookie(name) {   var cookieName = name + "="   var docCookie = document.cookie   var cookieStart   var end   if (docCookie.length>0) {      cookieStart = docCookie.indexOf(cookieName)      if (cookieStart != -1) {         cookieStart = cookieStart + cookieName.length         end = docCookie.indexOf(";",cookieStart)         if (end == -1) {            end = docCookie.length         }         return unescape(docCookie.substring(cookieStart,end))      }   }   return false}

example of usage:

getCookie('cookiename');

Hope this helps.

Cheers,CoolSmoothie