Hi !!!
I have a great css switcher ( javascript ) made by a friend, and I would like to add cookies to it, so that the user gets the same page everytime he comes in.
I have to do it in javascript. It’s complicated, but it HAS to be javascript ( I tried php, and I couldn’t get what I wanted ).
So, I wonder.
Is there a way I can write a cookie with this javascript function, and then get it back when the user comes back on the page ?
Sorry, my explanations are a bit messy…
I’ll show you the switcher.
[code=php]
function switchCSS(val){
document.getElementById(‘pStyle’).href=’css’+val+’.css’;
styleSheet=’css’+val+’.css’
that’s only one part, because the rest is not important.
in the head tag, I have this :
[code=php]
<link rel=”stylesheet” href=”css1.css” type=”text/css” id=”pStyle”>
A cookie would be great. On my previous try, i had cookies, and a cookiecheck file who would do the job, but the rest was not okay with my website…
Any help would be awesome !!!
Thanks !
Ness
I have to do it in javascript. It's complicated, but it HAS to be javascript [/QUOTE]
<i>
</i>var expiration=new Date();
expiration.setFullYear(expiration.getFullYear() + 1);
expiration=expiration.toGMTString();
document.cookie('cookieName=cookieData;expires='+expiration);
<i>
</i>var diedate=new Date();
diedate.setFullYear(diedate.getFullYear() - 1);
diedate=diedate.toGMTString();
document.cookie('cookieName=;expires='+diedate);
<i>
</i>function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { // if the cookie exists
offset += search.length
end = document.cookie.indexOf(";", offset); // set the index of beginning value
<i> </i>if (end == -1) // set the index of the end of cookie value
<i> </i> end = document.cookie.length;
<i> </i> returnvalue=unescape(document.cookie.substring(offset, end))
<i> </i> }
}
return returnvalue;
}
You're misinformed. It's very possible to write cookies in Javascript.
Use document.cookie('cookieName=cookieData'); to create a simple session cookie. For persistent cookies, you add an expiration date next, same way as PHP does things. The date has to be formatted in a certain way:
Wdy, DD-Mon-YYYY HH:MM:SS GMT
Or you can set it using the date() object. This sets the expiration date to one year from the current date, then sets a persisten cookie:
<i>
</i>var expiration=new Date();
expiration.setFullYear(expiration.getFullYear() + 1);
expiration=expiration.toGMTString();
document.cookie('cookieName=cookieData;expires='+expiration);
These are all the parameters you can set for cookies:
name=value; expires=date; path=pathname; domain=domainname; secure=bool
Only the name=value pair is required. To delete the cookie set the expiration date to sometime in the past:
<i>
</i>var diedate=new Date();
diedate.setFullYear(diedate.getFullYear() - 1);
diedate=diedate.toGMTString();
document.cookie('cookieName=;expires='+diedate);
You CAN read the cookies in Javascript, but it's a lot more complicated than in PHP. PHP stores all cookies for that document in an array, Javascript is just one long string, so you have to use substr and length quite a bit, but here's a function I use to search for a get cookie data in Javascript:
<i>
</i>function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { // if the cookie exists
offset += search.length
end = document.cookie.indexOf(";", offset); // set the index of beginning value
<i> </i>if (end == -1) // set the index of the end of cookie value
<i> </i> end = document.cookie.length;
<i> </i> returnvalue=unescape(document.cookie.substring(offset, end))
<i> </i> }
}
return returnvalue;
}
When you run, say, get_cookie('cookieName'), you get back either the data in that cookie, or an empty string if that cookie doesn't exist.
Hope this helps ?[/QUOTE]
<i>
</i>document.cookie('val=data;expires='+expiration);
<i>
</i>if(get_cookie('val')==""){ code to execute if there is no cookie }else{ do something with the output from get_cookie('val') }
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Les Enfants de l'Ô - version 3.0 - Mayi</title>
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="css1.css" type="text/css" id="pStyle">
<base target="iframe1">
<script src="index.js"></script>
<script type="text/javascript">
<!--
function P7_writeStyles(op,a){ //v1.5 by PVII
if(op==0||document.getElementById){var tS="<sty"+"le type="text/css">";
tS+=a+"<"+"/sty"+"le>";document.write(tS);document.close();}
}
P7_writeStyles(1,'.closed ul{display:none;}.open ul{display:block;}');
var styleSheet='css1.css';
var startcolor= new Array(255,255,255); // start color (red, green, blue)
var endcolor=new Array(0,0,0); // end color (red, green, blue)
//cookie stuff here
var expiration=new Date();
expiration.setFullYear(expiration.getFullYear() + 1);
expiration=expiration.toGMTString();
function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { // if the cookie exists
offset += search.length
end = document.cookie.indexOf(";", offset); // set the index of beginning value
if (end == -1) // set the index of the end of cookie value
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}
function cookie() {
if(get_cookie('val')==""){
document.getElementById('pStyle').href='css1.css';
styleSheet='css'+val+'.css'
}
else {
document.getElementById('pStyle').href='css'+get_cookie('val')+'.css';
styleSheet='css'+val+'.css'
}
}
//-->
</script>
<!--[if gte IE 5]>
<style>
#p7swapmenu ul a {height: 1em;}
</style>
<![endif]-->
<!--[if IE 5]>
<style>
#p7swapmenu li {margin-top: -3px;}
</style>
<![endif]-->
</head>
<body class="body" onload="document.urlForm.cUrl.value='http://psychomoa.free.fr/'+document.getElementById('iframe1').src;cookie()">
<div id="titre"></div>
<div id="logo"></div>
<div id="p7swapmenu">
<ul>
<li id="menu"></li>
<li class="closed" id="p1"><a href="#" onClick="P7_swapClass(1,'p1','open','closed','li');return false" target="_top"><img src="plus.gif" border="0">
Histoire</a>
<ul>
<li><a href="histoire1.htm" onclick="document.urlForm.cUrl.value=this.href">Première Partie <img src="moins.gif" border="0"></a></li>
<li><a href="histoire2.htm" onclick="document.urlForm.cUrl.value=this.href">Deuxième Partie <img src="moins.gif" border="0"></a></li>
<li><a href="histoire3.htm" onclick="document.urlForm.cUrl.value=this.href">Annexes <img src="moins.gif" border="0"></a></li>
</ul>
</li>
<li class="closed" id="p2"><a href="#" onClick="P7_swapClass(1,'p2','open','closed','li');return false"><img src="plus.gif" border="0">
Personnages</a>
<ul>
<li><a href="#">Première Partie <img src="moins.gif" border="0"></a></li>
<li><a href="#">Deuxième Partie <img src="moins.gif" border="0"></a></li>
<li><a href="#">Généalogie <img src="moins.gif" border="0"></a></li>
<li><a href="#">Quickref <img src="moins.gif" border="0"></a></li>
</ul>
</li>
<li class="closed" id="p3"><a href="#" onClick="P7_swapClass(1,'p3','open','closed','li');return false"><img src="plus.gif" border="0">
Galerie</a>
<ul>
<li><a href="#">Première Partie <img src="moins.gif" border="0"></a></li>
<li><a href="#">Deuxième Partie <img src="moins.gif" border="0"></a></li>
<li><a href="#">Wallpapers <img src="moins.gif" border="0"></a></li>
<li><a href="#">Autres <img src="moins.gif" border="0"></a></li>
</ul>
</li>
<li class="closed" id="p5"><a href="#" onClick="P7_swapClass(1,'p5','open','closed','li');return false"><img src="plus.gif" border="0">
Bonus</a>
<ul>
<li><a href="chat.htm">Tagboard <img src="moins.gif" border="0"></a></li>
<li><a href="#">ß-Lectrices <img src="moins.gif" border="0"></a></li>
<li><a href="#">Chapitres Bonus <img src="moins.gif" border="0"></a></li>
<li><a href="#">Tentacule Déçu <img src="moins.gif" border="0"></a></li>
<li><a href="#">Casting <img src="moins.gif" border="0"></a></li>
<li><a href="#">Espace Membres <img src="moins.gif" border="0"></a></li>
</ul>
</li>
<li class="closed" id="p4"><a href="#" onClick="P7_swapClass(1,'p4','open','closed','li');return false"><img src="plus.gif" border="0">
Divers</a>
<ul>
<li><a href="#">Livre de Platine <img src="moins.gif" border="0"></a></li>
<li><a href="version2.htm">Site <img src="moins.gif" border="0"></a></li>
<li><a href="faq.htm">F.A.Q. <img src="moins.gif" border="0"></a></li>
<li><a href="#">Remerciements <img src="moins.gif" border="0"></a></li>
<li><a href="#">Plan du Site <img src="moins.gif" border="0"></a></li>
<li><a href="#">NesS <img src="moins.gif" border="0"></a></li>
</ul>
</li>
<li class="closed" id="p6"><a href="#" onClick="P7_swapClass(1,'p6','open','closed','li');return false"><img src="plus.gif" border="0">
Liens</a>
<ul>
<li><a href="chat.htm">Lier LEÔ <img src="moins.gif" border="0"></a></li>
<li><a href="#">Mailing-List <img src="moins.gif" border="0"></a></li>
<li><a href="#">Romans en Ligne <img src="moins.gif" border="0"></a></li>
<li><a href="#">Sites d'Amis <img src="moins.gif" border="0"></a></li>
<li><a href="#">Divers <img src="moins.gif" border="0"></a></li>
</ul>
</li>
<li id="bas"></li>
</ul>
</div>
<div id="titrenews"></div>
<div id="newsbas"></div>
<div id="news">
<script src="scroller.js"></script>
</div>
<div id="frame">
<iframe id="iframe1" name="iframe1" width="100%" height="100%" frameborder="0" src="edito2.htm"></iframe>
</div>
[/code]
[code=php]
<div id="version">
<a href="#" target="IFrame2" onmousedown="sTop=top.frames['iframe1'].document.getElementsByTagName('body')[0].scrollTop" onclick="switchCSS(1);return false">White</a>
<!-- <a href="versioncheck2.php?newskin=1" target="IFrame2" onmousedown="sTop=top.frames['iframe1'].document.getElementsByTagName('body')[0].scrollTop" onclick="switchCSS(1);">White</a> -->
<a href="#" target="IFrame2" onmousedown="sTop=top.frames['iframe1'].document.getElementsByTagName('body')[0].scrollTop" onclick="switchCSS(2);return false">Violet</a>
<!-- <a href="versioncheck2.php?newskin=2" target="IFrame2" onmousedown="sTop=top.frames['iframe1'].document.getElementsByTagName('body')[0].scrollTop" onclick="switchCSS(2);">Violet</a> -->
<a href="#" target="IFrame2" onmousedown="sTop=top.frames['iframe1'].document.getElementsByTagName('body')[0].scrollTop" onclick="switchCSS(3);return false">Orange</a>
<!-- <a href="versioncheck2.php?newskin=3" target="IFrame2" onmousedown="sTop=top.frames['iframe1'].document.getElementsByTagName('body')[0].scrollTop" onclick="switchCSS(3);">Orange</a> -->
<a href="#" target="IFrame2" onmousedown="sTop=top.frames['iframe1'].document.getElementsByTagName('body')[0].scrollTop" onclick="switchCSS(4);return false">Bleu</a>
<!-- <a href="versioncheck2.php?newskin=4" target="IFrame2" onmousedown="sTop=top.frames['iframe1'].document.getElementsByTagName('body')[0].scrollTop" onclick="switchCSS(4);">Bleu</a> -->
<a href="#" target="IFrame2" onmousedown="sTop=top.frames['iframe1'].document.getElementsByTagName('body')[0].scrollTop" onclick="switchCSS(5);return false">Vert</a>
<!-- <a href="versioncheck2.php?newskin=5" target="IFrame2" onmousedown="sTop=top.frames['iframe1'].document.getElementsByTagName('body')[0].scrollTop" onclick="switchCSS(5);">Vert</a> -->
</div>
<div id="versiontitre"></div>
<form name="urlForm" action="#">
<input type="hidden" name="cUrl" value="">
</form>
<script language="JavaScript" type="text/javascript">
<!--
var sTop;
var ns=(!document.all&&(navigator.vendor=='Netscape'||navigator.vendor=='Netscape6')&&!window.opera)
//window.onerror=function(e){if(ns)return true;}
//window.onerror=function(e){if(ns)alert(e);return true;}
function switchCSS(val){
document.getElementById('pStyle').href='css'+val+'.css';
document.cookie('val=data;expires='+expiration);
styleSheet='css'+val+'.css'
if (val==1)
{
strt='Mayi';
startcolor= new Array(255,255,255); // start color (red, green, blue)
endcolor=new Array(0,0,0); // end color (red, green, blue)
}
if (val==2)
{
strt='Asla';
startcolor= new Array(153,102,255); // start color (red, green, blue)
endcolor=new Array(0,0,0); // end color (red, green, blue)
}
if (val==3)
{
strt='Okhanan';
startcolor= new Array(255,153,51); // start color (red, green, blue)
endcolor= new Array(0,0,0); // end color (red, green, blue)
}
if (val==4)
{
strt='Isaac';
startcolor= new Array(102,102,255); // start color (red, green, blue)
endcolor=new Array(0,0,0); // end color (red, green, blue)
}
if (val==5)
{
strt='Lúka';
startcolor= new Array(153,255,153); // start color (red, green, blue)
endcolor=new Array(0,0,0); // end color (red, green, blue)
}
document.title="Les Enfants de l'Ô - version 3.0 - "+strt;
if(ns){
if(typeof(top.frames['iframe1'])=='object'){
document.getElementById('iframe1_1').src =document.urlForm.cUrl.value;
//alert(top.frames[0].document.getElementById('p1').nodeName)
top.frames[0].document.getElementById('cStyle').href='css'+val+'.css';
//document.getElementById('iframe1_1').document.getElementById('cStyle').href='css'+val+'.css';
}
}
else{
sTop+=0;
top.frames['iframe1'].document.getElementById('cStyle').href='http://psychomoa.free.fr/css'+val+'.css';
if(navigator.vendor=='Firefox'){
setTimeout("top.frames['iframe1'].window.scrollTo(0,"+sTop+")",250);
}
else{
top.window.frames['iframe1'].window.scrollTo(0,sTop)
}
}
top.window.frames['IFrame2'].location.href='versioncheck2.php?newskin='+val;
}
//-->
</script>
<iframe name='IFrame2' id='IFrame2' style="width:0;height:0;border:none;" frameborder="0"></iframe>
</body>
</html>
[/code]
[code=php]
<link rel="stylesheet" href="css1.css" type="text/css" id="pStyle">
[/code]
[code=php]
var styleSheet='css1.css'; // that's javascript
[/code]
<i>
</i><script type="text/javascript">
if(get_cookie('val')==""){
document.write("<link rel="stylesheet" href="css1.css" type="text/css" id="pStyle">");
}else{
document.write("<link rel="stylesheet" href="css"+get_cookie('val')+".css" type="text/css" id="pStyle">");
}
</script>
0.1.9 — BETA 6.3