Forum Moderators: open

Message Too Old, No Replies

Cookie Reset

A short Javascript with a small problem

         

dave040101

12:58 am on Dec 7, 2007 (gmt 0)

10+ Year Member



I have a short javascript that allows me to display images in order using a next and prev mouse click option, however, it is missing one small piece. I would like the cookies to be reset once the browser closes so that when a user returns to the initial page the image display will reset to picture 1 instead of picking up where it was when the browser closed. Below is the script. Thanks

function getCookie(name) {
var index = document.cookie.indexOf(name + "=");
if (index == -1) return null;
index = document.cookie.indexOf("=", index) + 1; // first character
var endstr = document.cookie.indexOf(";", index);
if (endstr == -1) endstr = document.cookie.length; // last character
return unescape(document.cookie.substring(index, endstr));
}

function setCookies(how) {
var expiresDate = new Date();
expiresDate.setFullYear(expiresDate.getFullYear() + 1);

var number = getCookie("imageNumber")

if(number == null) {
document.cookie = "imageNumber=1; expires=" + expiresDate.toUTCString() + "; path=/";
}
else if(how == "Up") {
document.cookie = "imageNumber=" + (parseInt(number) + 1) + "; expires=" + expiresDate.toUTCString() + "; path=/";
}

else if(how == "Down") {
document.cookie = "imageNumber=" + (parseInt(number) - 1) + "; expires=" + expiresDate.toUTCString() + "; path=/";
}
}

function writePrev() {
document.write("<a href=\"index.html\" onClick=setCookies(\"Down\");><-- Prev</a>");
}

function writeImage() {
document.write("<img src=\"" + getCookie("imageNumber") + ".jpg\">");
}

function writeNext() {
document.write("<a href=\"index.html\" onClick=setCookies(\"Up\");>Next --></a>");
}

daveVk

2:10 am on Dec 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if you remove the expires= part, I understand the cookie is not saved when the browser is closed.