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> Code (markup): 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"> Code (markup): 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.
<body id="page" <?php if ($_COOKIE['cookie value'] == 'normal') echo 'class="cookie value"'; else echo 'class="font-small width-fluid"';?>> Code (markup):
er.. dont you think you are mixing up javascript and php? you didnt 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. 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')" PHP: 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')" PHP: It should work dont think i did anythink wrong. Joe