Click to See Complete Forum and Search --> : Image Cookie


96turnerri
01-10-2004, 09:38 AM
hi i would like a cookie to be placed onclick and read onload and then displayed when page is reloaded heres what i mean exactly

<a href="#" onclick="document.images[1].src='xx.jpg'; return false;">Change Image</a>

so the second image on page is changed on clicking this

im kinda after something like this

<body onload="getCookie();">

<a href="#" onclick="document.images[1].src='xx.jpg'; putCookie(); return false;">Change Image</a>

the idea of this is to remember last image someone has selected and then read cookie onload and display that image next time they visit

help would be much appreciated on this as i have been trying to do it for two days now, tried modifying someones background cookie script but had no luck with that, heres that script anyway if someone would like to try editing that

Thanks
96

Khalid Ali
01-10-2004, 09:42 AM
have u used cookies to store data before????

96turnerri
01-10-2004, 09:55 AM
yes i used them once on a site to store data on a form name and email, but no longer have that code to look at, and im using on my current project what this is for for last stylesheet chosen

thanks for your help khalid

Khalid Ali
01-10-2004, 10:11 AM
no problems..;)

Take a look at this resource (http://www.webapplikations.com/pages/html_js/document/SimpleLoginCookieScript.html)

You can see how cookie may store data,now what you may need to do is incorporate your code

<a href="#" onclick="document.images[1].src='xx.jpg'; putCookie(); return false;">Change Image</a>

in a manner that putCookie method takes one prameter that is the image src which is being displayed right now you can do that as

<a href="#" onclick="document.images[1].src='xx.jpg'; putCookie(document.images[1].src); return false;">Change Image</a>

make sure yu make changes to resource I posted aboves cookie writing method so that it can get value from putCookie

96turnerri
01-11-2004, 09:17 AM
ok ive got this code and it puts the cookie fine, but how do i get i to read it on page load and display that image in cookie?


<SCRIPT LANGUAGE="JavaScript">
var href = document.location.href;
var loc = href.split("/");
var addy = ""
if (loc[0] == "http:") {addy = "/" + loc[3] + "/";}
else addy = "/" + loc[4] + "/";
</script>

<SCRIPT LANGUAGE = "JavaScript">
function getImage(){
var favimage = getCookie('image');

if (favimage == null) {
setCookie('image', favimage, exp);
}
document.images[18].src=favimage;
}

function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function getCookie () {
var arg = "image" + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function setCookie(cookieName,cookieValue) {
var today = new Date();
var expire = new Date();
expire.setTime(today.getTime() + 3600000*24*365);
document.images[18].src=cookieValue;
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString();
}

function deleteCookie() {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = '';
document.images[18].src='';
document.cookie = "image="+ cval +"; expires=" + exp.toGMTString();
}
</SCRIPT>


<a href="#" onclick="setCookie(); return false;">Set</a>

Thanks 96

Khalid Ali
01-11-2004, 10:50 AM
Hey 96 take a look,I have put together this page (http://www.webapplikations.com/pages/html_js/image_examples/LoadPreviouslySelectedImage.html)

96turnerri
01-11-2004, 11:51 AM
thank you for that, it looks like jst what i need, i will try modifying it to fit my needs, let you know how i get on should be ok tho hopefully lol, thanks again

Khalid Ali
01-11-2004, 12:10 PM
My pleasure....:cool: