1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
| <?php header('Content-type: text/html; charset=UTF-8');
$fichier_courant = substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],'.'));
$name_cookie = $fichier_courant == '/index' ? $_SERVER['SERVER_NAME'] : $fichier_courant;
if(isset($_COOKIE[$name_cookie]))
{
$cookie_affiche_div = (get_magic_quotes_gpc())? stripslashes($_COOKIE[$name_cookie]) : $_COOKIE[$name_cookie];
$tab_affiche_div = unserialize($cookie_affiche_div);
if(is_array($tab_affiche_div))
{
natsort($tab_affiche_div);
foreach ($tab_affiche_div as $key => $value)
echo $key. ' = ' .$value. '<br />';
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Afficher ou masquer un DIV et garder son état en mémoire en Javascript</title>
<script type="text/javascript">
<!--
function ShowDiv () {
//tab_argumemts = tableau des arguments (=id des div) passés lors de l'appel de la fonction ShowDiv
var tab_argumemts = ShowDiv.arguments;
// Si plus d'un id donné en argument on appelle la fonction AmtShowDiv en lui passant le tableau des arguments sinon l'id correspond au premier (et dernier) élément du tableau.
if (tab_argumemts.length > 1) AmtShowDiv (tab_argumemts); else var id = tab_argumemts[0];
var id_show;
//Si l'id correspond à un id existant
if (id_show = document.getElementById(id))
{
// Inverse l'état du div : s'il est à "display : none", sa largeur id_show.offsetWidth = 0
var showHide = id_show.offsetWidth > 0 ? 'none' : 'block';
id_show.style.display = showHide;
var tab_cook_showDiv;
// Cherche le cookie affecté à cette page et s'il existe le désérialise
tab_cook_showDiv = (tab_cook_showDiv = GetCookie(SetNomCookie())) ? Unserialize(tab_cook_showDiv) : null;
// Vérifie que le résultat tab_cook_showDiv est un tableau sinon initialise cette variable en tableau
tab_cook_showDiv = (typeof tab_cook_showDiv == 'object' && tab_cook_showDiv instanceof Array) ? tab_cook_showDiv : new Array();
// Enregistre l'état de la div avec l'id comme index de l'élément dans le tableau (crée l'élément ou le remplace)
tab_cook_showDiv[id] = showHide;
// Envoi le tableau sérialisé dans un cookie dont le nom est "SetNomCookie()"
SetCoockie (SetNomCookie(), Serialize(tab_cook_showDiv));
}
}
function InitShowDiv () {
var cookie_showDiv;
// Cherche le cookie affecté à cette page (ayant pour nom SetNomCookie()) et s'il existe ...
if (cookie_showDiv = GetCookie(SetNomCookie()))
{
// Désérialise le tableau enregistré dans le cookie
var tab_cook_showDiv = Unserialize(cookie_showDiv);
// Si tab_cook_showDiv est un objet et un tableau
if(typeof tab_cook_showDiv == 'object' && tab_cook_showDiv instanceof Array)
{
// Affiche les div suivant leur état "block" ou "none" enregistrés dans le tableau du cookie de la page)
// Liste le tableau associatif
for (var id in tab_cook_showDiv)
{
var affich_id;
// Si l'index de l'élément du tableau correspond à un id existant de la page en cours => affecte son affichage avec sa valeur enregistrée
if (affich_id = document.getElementById(id)) affich_id.style.display = tab_cook_showDiv[id];
}
// Renvoie le même cookie pour prolongation de sa durée de vie dès le chargement de la page
SetCoockie (SetNomCookie(), cookie_showDiv);
}
}
}
function addLoadInitShowDiv(func) {
if (window.addEventListener)
{
window.addEventListener("load", func, false);
}
else if (document.addEventListener)
{
document.addEventListener("load", func, false);
}
else if (window.attachEvent)
{
window.attachEvent("onload", func);
}
}
addLoadInitShowDiv(InitShowDiv);
function AmtShowDiv (tab_argumemts) {
//tab_argumemts = Tableau des arguments (=id des div) transmis par la fonction ShowDiv (si le nombre des arguments > 1)
// Crée une variable composée de la concaténation des id des div passés en paramètre lors de l'appel de la fonction.
//Cette variable permet d'identifier des appels à la fonction provenant de blocs différents
var div_concat = '';
for (var i=0; i < tab_argumemts.length; i++) {div_concat += tab_argumemts[i];}
/* Création d'une propriété (avec le mot-clé this) se référent à elle-même, ce qui revient à créer une variable qui mémorisera sa dernière valeur définie lors d'appels succesifs.
Cela permettra la mémorisation lors d'appels successifs dans une même session y compris sans les cookies activés dans le navigateur, mais dans ce cas la mémorisation de l'état sera perdue au rechargement de la page puisque c'est un cookie qui mémorise l'état d'une session à un autre. */
this.EtatAmt = typeof this.EtatAmt != 'undefined' ? this.EtatAmt : new Array;
this.EtatAmt[div_concat] = typeof this.EtatAmt[div_concat] != 'undefined' ? this.EtatAmt[div_concat] : 'none';
var tab_cook_showDiv;
// Cherche le cookie affecté à cette page et s'il existe le désérialise
tab_cook_showDiv = (tab_cook_showDiv = GetCookie(SetNomCookie())) ? Unserialize(tab_cook_showDiv) : null;
// Vérifie que le résultat tab_cook_showDiv est un tableau sinon initialise cette variable en tableau
tab_cook_showDiv = (typeof tab_cook_showDiv == 'object' && tab_cook_showDiv instanceof Array) ? tab_cook_showDiv : new Array;
// Cherche l'élément 'div_concat' du tableau du cookie sinon le crée et lui affecte la valeur this.EtatAmt[div_concat]
tab_cook_showDiv[div_concat] = (typeof tab_cook_showDiv[div_concat] != 'undefined')? tab_cook_showDiv[div_concat] : this.EtatAmt[div_concat];
// Cherche si les div passés en argument dans la fonction AmtShowDiv ne sont pas tous déjà dans l'état inverse de la valeur de tab_cook_showDiv[div_concat] (si modifiés individuellement par la fonction ShowDiv ou par css)
var affiche_id;
// Valeur témoin
var verif = false;
// Liste les arguments passés en paramètres lors de l'appel de la fonction AmtShowDiv
for (var i=0; i < tab_argumemts.length; i++)
{
// Si l'argument passé dans la fonction correspond à un id existant de la page en cours
if (affiche_id = document.getElementById(tab_argumemts[i]))
{
// Si cet élément est dans le même état que tab_cook_showDiv[div_concat], verif = true
//(si au moins un élément est dans le même état, alors tous ne sont pas dans l'état inverse)
if ((tab_cook_showDiv[div_concat] == 'block' && affiche_id.offsetWidth > 0) || (tab_cook_showDiv[div_concat] == 'none' && !(affiche_id.offsetWidth > 0))) verif = true;
}
}
// si vérif = true => redéfini tab_cook_showDiv[div_concat] pour inverser l'affichage
if (verif === true) tab_cook_showDiv[div_concat] = tab_cook_showDiv[div_concat] == 'block' ? 'none' : 'block';
// Défini l'affichage des blocs (en utilisant les id des div passés en paramètres lors de l'appel de la fonction)
// Liste les arguments passés dans la fonction AmtShowDiv
for (var i=0; i < tab_argumemts.length; i++)
{
// Si l'argument passé en paramètre dans la fonction AmtShowDiv correspond à un id existant de la page en cours => affecte son affichage "style.display" avec la valeur de tab_cook_showDiv[div_concat]
if (affiche_id = document.getElementById(tab_argumemts[i]))
{
affiche_id.style.display = tab_cook_showDiv[div_concat];
// Et enregistre son état dans le tableau
tab_cook_showDiv[tab_argumemts[i]] = tab_cook_showDiv[div_concat];
}
}
// Enregistre la valeur de tab_cook_showDiv[div_concat] dans this.EtatAmt[div_concat] (pour mémorisation de l'état dans une même session y compris sans les cookies activés)
this.EtatAmt[div_concat] = tab_cook_showDiv[div_concat];
// Envoi le tableau sérialisé dans un cookie dont le nom est "SetNomCookie()"
SetCoockie (SetNomCookie(), Serialize(tab_cook_showDiv));
}
function SetNomCookie() {
var fich = window.location.pathname;
if (fich != '/')
{
//Enlève tout ce qu'il y a après de dernier point
fich = fich.substr(0,fich.lastIndexOf('.'));
//Si fich = "/index" on est sur la page d'accueil et l'on renvoie le nom de domaine
var name = (fich == '/index')? window.location.host : fich;
}
else
{
//si fich == '/' on est sur la page d'accueil et l'on renvoie le nom de domaine
var name = window.location.host;
}
return (name);
}
function SetCoockie (nom, valeur) {
var peremption = 1000*60*60*24*365;//durée de validité : 1an
var maintenant = new Date();
var temps = new Date(maintenant.getTime() + peremption);
document.cookie = nom+"="+escape(valeur)+"; expires="+temps.toGMTString()+";";
}
function GetCookie(nom) {
var deb,fin;
deb = document.cookie.indexOf(nom + "=");
if (deb >= 0)
{
deb += nom.length + 1;
fin = document.cookie.indexOf(";",deb);
if (fin < 0) fin = document.cookie.length;
return unescape(document.cookie.substring(deb,fin));
}
else return false;
}
function Serialize (txt) {
/*auteur : XoraX
info : <a href="http://www.xorax.info/blog/programmatio" target="_blank">http://www.xorax.info/blog/programmatio</a> ... e-php.html ... script légèrement modifié*/
switch(typeof(txt))
{
case 'string':
return 's:'+txt.length+':"'+txt+'";';
case 'number':
if(txt>=0 && String(txt).indexOf('.') == -1 && txt < 65536) return 'i:'+txt+';';
return 'd:'+txt+';';
case 'boolean':
return 'b:'+( (txt)?'1':'0' )+';';
case 'object':
var i=0,k,ret='';
for(k in txt)
{
//alert(isNaN(k));
if(!isNaN(k)) k = Number(k);
ret += Serialize(k)+Serialize(txt[k]);
i++;
}
return 'a:'+i+':{'+ret+'}';
default:
return 'N;';
//alert('var undefined: '+typeof(txt));return undefined;
}
}
function Unserialize(txt){
/*auteur : XoraX
info : <a href="http://www.xorax.info/blog/programmatio" target="_blank">http://www.xorax.info/blog/programmatio</a> ... e-php.html ... script légèrement modifié*/
var level=0,arrlen=new Array(),del=0,final=new Array(),key=new Array(),save=txt;
while(1)
{
switch(txt.substr(0,1))
{
case 'N':
del = 2;
ret = null;
break;
case 'b':
del = txt.indexOf(';')+1;
ret = (txt.substring(2,del-1) == '1')?true:false;
break;
case 'i':
del = txt.indexOf(';')+1;
ret = Number(txt.substring(2,del-1));
break;
case 'd':
del = txt.indexOf(';')+1;
ret = Number(txt.substring(2,del-1));
break;
case 's':
del = txt.substr(2,txt.substr(2).indexOf(':'));
ret = txt.substr( 1+txt.indexOf('"'),del);
del = txt.indexOf('"')+ 1 + ret.length + 2;
break;
case 'a':
del = txt.indexOf(':{')+2;
ret = new Array();
arrlen[level+1] = Number(txt.substring(txt.indexOf(':')+1, del-2))*2;
break;
case 'O':
txt = txt.substr(2);
var tmp = txt.indexOf(':"')+2;
var nlen = Number(txt.substring(0, txt.indexOf(':')));
name = txt.substring(tmp, tmp+nlen );
//alert(name);
txt = txt.substring(tmp+nlen+2);
del = txt.indexOf(':{')+2;
ret = new Object();
arrlen[level+1] = Number(txt.substring(0, del-2))*2;
break;
case '}':
txt = txt.substr(1);
if(arrlen[level] != 0)
{
//alert('var missed : '+save);
return undefined;
}
//alert(arrlen[level]);
level--;
continue;
default:
if(level==0) return final;
//alert('syntax invalid(1) : '+save+"\nat\n"+txt+"level is at "+level);
return undefined;
}
if(arrlen[level]%2 == 0)
{
if(typeof(ret) == 'object')
{
//alert('array index object no accepted : '+save);
return undefined;
}
if(ret == undefined)
{
//alert('syntax invalid(2) : '+save);
return undefined;
}
key[level] = ret;
}
else
{
var ev = '';
for(var i=1;i<=level;i++)
{
if(typeof(key[i]) == 'number')
{
ev += '['+key[i]+']';
}
else
{
ev += '["'+key[i]+'"]';
}
}
eval('final'+ev+'= ret;');
}
arrlen[level]--;//alert(arrlen[level]-1);
if(typeof(ret) == 'object') level++;
txt = txt.substr(del);
continue;
}
}
-->
</script>
<style type="text/css">
.inverse_aff {
cursor:pointer;
text-decoration:underline;
}
#conteneur {
margin-top:2em;
padding:2em;
border:5px solid green;
}
#conteneur div {
border:1px solid #CCCCCC;
}
</style>
</head>
<body>
<p class="inverse_aff" onclick = "ShowDiv('conteneur')"><strong>Afficher / masquer le bloc conteneur</strong></p>
<div id = "conteneur">
<p class="inverse_aff" onclick = "ShowDiv('div_A')">Afficher / masquer div A</p>
<div id = "div_A" >AAAAAAAAAAAAA </div>
<p class="inverse_aff" onclick = "ShowDiv('div_B')" >Afficher / masquer div B</p>
<div id = "div_B">BBBBBBBBBBBBBB </div>
<p class="inverse_aff" onclick = "ShowDiv('div_C')">Afficher / masquer div C</p>
<div id = "div_C" >CCCCCCCCCCCCC </div>
<br />
<p class="inverse_aff" onclick = "ShowDiv('div_D','div_E','div_F')"><strong>Afficher / masquer les div D, E, F</strong></p>
<p class="inverse_aff" onclick = "ShowDiv('div_D')">Afficher / masquer div D</p>
<div id = "div_D" >DDDDDDDDDDDDD </div>
<p class="inverse_aff" onclick = "ShowDiv('div_E')">Afficher / masquer div E</p>
<div id = "div_E" >EEEEEEEEEEEEE </div>
<p class="inverse_aff" onclick = "ShowDiv('div_F')">Afficher / masquer div F</p>
<div id = "div_F" >FFFFFFFFFFFFF </div>
<br />
<p class="inverse_aff" onclick = "ShowDiv('div_G','div_H')"><strong>Afficher / masquer les div G, H</strong></p>
<p class="inverse_aff" onclick = "ShowDiv('div_G')">Afficher / masquer div G</p>
<div id = "div_G" >GGGGGGGGGGGGG </div>
<p class="inverse_aff" onclick = "ShowDiv('div_H')">Afficher / masquer div H</p>
<div id = "div_H" >HHHHHHHHHHHHH </div>
</div>
<p>Autre bloc de texte</p>
</body>
</html> |
Partager