Bonjour, je suis nouveau sur le forum et débutant en développement web.
Je crée mon site (http://www.aromasie.com) sous joomla et je souhaite adapter le template en fonction de la résolution. Pour cela, j'utilise une méthode AJAX mise au point par un certain Cr@zy:
extrait de index.php :
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
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 <script type="text/javascript" src="resscreen.js"></script> <?php switch (ereg("MSIE", $_SERVER["HTTP_USER_AGENT"])) { case True: /* navigateur = IE */ /*echo "IE";*/ if ($_COOKIE['res_width'] > 1200) { /*Résolution grande*/ $Chemin_css = "...template_grand_IE.css"; echo ($_COOKIE['res_width']); } else { /*Résolution petite*/ $Chemin_css = "...template_petit_IE.css"; echo ($_COOKIE['res_width']); } break; case False: /* Navigateur != IE*/ /*echo "Autre";*/ if ($_COOKIE['res_width'] > 1200) { /*Résolution grande*/ $Chemin_css = "...template_grand.css"; echo ($_COOKIE['res_width']); } else { /*Résolution petite*/ $Chemin_css = "...template_petit.css"; echo ($_COOKIE['res_width']); } break; } ?> <link rel="stylesheet" href=<?php echo $Chemin_css?> type="text/css" />
Voici le code javascript (resscreen.js) :
et enfin resscreen.php :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 function Xhr_ResScreen(file){ if( window.XMLHttpRequest ){ xhr = new XMLHttpRequest(); if ( xhr.overrideMimeType ) xhr.overrideMimeType('text/html; charset=ISO-8859-1'); } else { if ( window.ActiveXObject ){ try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { window.alert("Votre navigateur ne prend pas en charge l'objet XMLHTTPRequest."); } } } } if( (new RegExp("[?]", "gi")).test(file) ){ xhr.open("GET", file + '&rand=' + Math.random(), true); } else { xhr.open("GET", file + '?rand=' + Math.random(), true); } xhr.setRequestHeader("Content-type", "charset=ISO-8859-1"); xhr.send(null); return xhr;//rajouté } var Requete_Ajax = Xhr_ResScreen('resscreen.php?width='+screen.width+'&height='+screen.height);
En local, la page affiche ceci lors du premier chargement :
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 $cookietime = 3600; // Récupération de la résolution de l'utilisateur $width = ( isset($_GET['width']) && is_numeric($_GET['width']) ) ? intval($_GET['width']) : ''; $height = ( isset($_GET['height']) && is_numeric($_GET['height']) ) ? intval($_GET['height']) : ''; if( !empty($width) && !empty($height) ){ $HTTP_COOKIE_VARS['res_width'] = $width; $HTTP_COOKIE_VARS['res_height'] = $height; setcookie("res_width", $HTTP_COOKIE_VARS['res_width'], time() + $cookietime, '/'); setcookie("res_height", $HTTP_COOKIE_VARS['res_height'], time() + $cookietime, '/'); }
La page affiche ceci :
Notice: Undefined index: res_width in C:....index.php on line 11
Notice: Undefined index: res_height in C....index.php on line 11
Notice: Undefined index: res_width in C:....index.php on line 12
Pourtant, la console (Firebug) affiche bien:
GET http:...resscreen.php?width=1680&height=1050 rand=0.717867140902937[/url]
GET http:...resscreen.php?width=1680&height=1050&rand=0.717867140902937[/url]
200 OK 65ms
et en entête:
Set-Cookieres_width=1680; expires=Fri, 16-Jul-2010 07:00:30 GMT; path =/
res_height=1050; expires=Fri, 16-Jul-2010 07:00:30 GMT; path =/
D'où ma question: Pourquoi mon script ne fonctionne-t-il qu'une fois la page rafraîchie? (Et cela autant sous IE que sur Firefox)
Y a-t-il un moyen de corriger cela? (sinon, je ne vois pas l'intérêt d'AJAX!)
Merci d'avance!
Partager