View Full Version : need help with javascript cookies.
FR3@K
Oct 4th 2007, 1:58 am
hello everyone.
if you can help me , then please help !
I am trying to set a cookie using onclick on href tag.
and i want to retrieve it when the page is loaded.
it should be set like this :
<a id="switchwidthwide" href="javascript:void(0)" onclick='setcookies("name of cookie", "value of cookie")' title="Wide width"></a>
i want it to be retrived in body tag like this :
<body id="page" if cookie value = normal , echo class="cookie value" else echo class="font-small width-fluid">
can someone please explain how can i do this?
i am new at javascript and php.
and someone told me that this thing will be done with javascript.
temp2
Oct 4th 2007, 5:00 am
<body id="page" <?php if ($_COOKIE['cookie value'] == 'normal') echo 'class="cookie value"'; else echo 'class="font-small width-fluid"';?>>
FR3@K
Oct 4th 2007, 5:23 am
er.. dont you think you are mixing up javascript and php?
you didnt tell me how to set the cookies using onclick.
joesgraphics
Oct 4th 2007, 6:47 am
er.. dont you think you are mixing up JavaScript and php?
you didn't tell me how to set the cookies using onclick.
Well the reason i think he got mixed up is since u used as Ur example echo etc that aint used in js but is in php.
To do what u are wanting in js you will need some functions to get, add cookies there are a few unready made js functions out there like here (http://www.quirksmode.org/js/cookies.html).
code to use with those functions would be:
<script type="text/javascript">
function addCookie(name, value) {
createCookie(name,value, 360);
}
if (!readCookie("body-size")) {
createCookie('body-size', 'width-fluid', 360);
document.getElementById('switchwidthwide').className = readCookie("body-size");
}
else {
document.getElementById('switchwidthwide').className = readCookie("body-size");
}
</script>
then on the onclick use onclick="addCookie('body-size', 'width-small')"
Or you could use mootools and use this code below for your example:
<script type="text/javascript" src="mootools.js"></script>
<script type="text/javascript">
function addCookie(name, value) {
Cookie.set(name, value, {duration: 360});
}
if (!Cookie.get("body-size")) {
Cookie.set('body-size', 'width-fluid', {duration: 360});
document.getElementById('switchwidthwide').className = Cookie.get("body-size");
}
else {
document.getElementById('switchwidthwide').className = Cookie.get("body-size");
}
</script>
then on the onclick use onclick="addCookie('body-size', 'width-small')"
It should work dont think i did anythink wrong.
Joe
FR3@K
Oct 5th 2007, 3:09 am
thanks joesgraphics , +rep for you, your code really works :D
joesgraphics
Oct 8th 2007, 4:59 am
Glad it works, thanks for the rep
Joe
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.