Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
67
rated 0 times [  68] [ 1]  / answers: 1 / hits: 93285  / 11 Years ago, wed, april 10, 2013, 12:00:00

I have this real strange problem with client side javascript setting cookies. I'm developing a little 1 page demo at the moment to use cookies to store some 'preferences'. Please note that I can't use a server side language for this demo or any 3rd party jQuery plugins.



So I've written a javascript object to set a cookie:



var cookie = {
set: function (name,value,exdays) {

var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var value = escape(value) + ((exdays==null) ? : ; expires=+exdate.toUTCString());
document.cookie=name + = + value;
console.log(document.cookie);
}
}

cookie.set('foo','bar',2);
console.log(document.cookie);


It just returns an empty string. I've gone into Chrome console to see if I can do it via directly modifying document.cookie



> document.cookie = foo=bar;
foo=bar
> document.cookie



How do you set a cookie via client side javascript?



Edit: I am not in incognito mode and cookies are enabled.


More From » cookies

 Answers
1

You can't set cookies by the look of things if its not running in a web server.



file:///C:/Users/me/Desktop/demo/demo.html



however:



http://localhost/demo/demo.html works.


[#79006] Monday, April 8, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jadyngraysons

Total Points: 455
Total Questions: 109
Total Answers: 98

Location: Trinidad and Tobago
Member since Fri, May 8, 2020
4 Years ago
;