This is one script that sets the cookie with some html file.
window.onload=init;
function init() {
var userName="";
if(document.cookie != "") {
username=document.cookie.split("=")[1];
document.getElementById("name_field").value = username;
}
document.getElementById("name_field").onblur = setCookie;
}
function setCookie() {
var exprDate = new Date();
exprDate.setMonth(exprDate.getMonth() + 6);
var username = document.getElementById("name_field").value;
document.cookie = "username=" + username + ";path=/;expires=" + exprDate.toGMTString();
}
This is another script with some different html file, (that had not saved a cookie in the past) that checks if there is a cookie saved with this document.
window.onload = initTest;
function initTest() {
if(document.cookie == "") alert("No,cookies stored !");
else alert("cookies found !");
}
To my surprise the result when i run the 2nd html file with the second script,is cookies found Why is that ? When that document has not saved a cookie then how come document.cookie != "" ?
Cookies are set per domain and/or path.
Examples:
http://www.example.com/foo.htmlCookie:x=x; max-age=3600;is visible athttp://www.example.com/*, but not athttp://other.example.com/http://www.example.com/foo.htmlCookie:x=x; max-age=3600; domain=.example.comis visible athttp://*.example.com/*andhttp://example.com/*x=x; max-age=3600; securex=x; max-age=3600; path=/