Hello tlm,
bon, je viens de faire un code utilisant de la POO javascript. Comme je ne suis pas un pro en la matière, je fais appel aux gourous JS pour critiquer un peu ma classe, et me donner des conseils pour améliorer qualitativement le code.
Le code complet se trouve ici :
http://www.phpcs.com/codes/PHP5-NOTIMEOUT-PACKAGE_39866.aspx
Ca peut servir pour la compréhension globale (c'est du requêtage xmlhttp, donc il y a un côté serveur).
Au passage, il y a aussi un petit soucis non réglé, mais je ne sais pas si c'est côté js ou côté PHP. Les données que je renvoie (traitées par PHP donc) le sont avec un header indiquant qu'il s'agit de html, avec un encodage en iso-8859-1. Néanmoins, si je renvoies du HTML, les balises ne sont pas interprétées.
Derniere question, histoire de : dans le cadre, donc, d'un encodage en iso-8859-1, la propriété de chaîne length compte-t-elle les caractères invisibles du type \n ? Parce que j'ai une différence notable, si je compte les octets d'un fichier (ou d'une chaîne) comportant des caractères invisibles via PHP, et via javascript.
Le code js en lui-même, je le copie ici, même s'il est un peu long :
/**
@author : Johan Barbier <johan.barbier@gmail.com>
@Version : 2006/10/09
*/
function noTimeOut () {
if (window.XMLHttpRequest) {
var oXmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
var oXmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var START = LIMIT = STEP = FILE = QUERY = TARGET = SCRIPT = MSG = '';
function getDefault (iStart) {
var j = iStart + STEP;
oXmlhttp.open('POST', SCRIPT);
oXmlhttp.onreadystatechange=function() {
if (oXmlhttp.readyState==1) {
if (MSG != '') {
parent.document.getElementById (MSG).firstChild.data = 'Loading';
}
}
if (oXmlhttp.readyState==2) {
if (MSG != '') {
parent.document.getElementById (MSG).firstChild.data = 'Loaded';
}
}
if (oXmlhttp.readyState==4 && oXmlhttp.status == 200) {
if (MSG != '') {
parent.document.getElementById (MSG).firstChild.data = 'OK';
}
parent.document.getElementById (TARGET).firstChild.appendData (oXmlhttp.responseText);
if (j < LIMIT) {
getDefault (j);
}
}
}
oXmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var data = 'sType=DEFAULT&iStart='+iStart+'&iStep='+STEP;
oXmlhttp.send (data);
}
function getDB (iStart) {
var j = iStart + STEP;
oXmlhttp.open('POST', SCRIPT);
oXmlhttp.onreadystatechange=function() {
if (oXmlhttp.readyState==1) {
if (MSG != '') {
parent.document.getElementById (MSG).firstChild.data = 'Loading';
}
}
if (oXmlhttp.readyState==2) {
if (MSG != '') {
parent.document.getElementById (MSG).firstChild.data = 'Loaded';
}
}
if (oXmlhttp.readyState==4 && oXmlhttp.status == 200) {
if (MSG != '') {
parent.document.getElementById (MSG).firstChild.data = 'OK';
}
parent.document.getElementById (TARGET).firstChild.appendData (oXmlhttp.responseText);
if (j < LIMIT) {
getDB (j);
}
}
}
oXmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var data = 'sType=DB&iStart='+iStart+'&iStep='+STEP+'&sQuery='+QUERY;
oXmlhttp.send (data);
}
function getFile (iStart) {
var j = iStart + STEP;
oXmlhttp.open('POST', SCRIPT);
oXmlhttp.onreadystatechange=function() {
if (oXmlhttp.readyState==1) {
if (MSG != '') {
parent.document.getElementById (MSG).firstChild.data = 'Loading';
}
}
if (oXmlhttp.readyState==2) {
if (MSG != '') {
parent.document.getElementById (MSG).firstChild.data = 'Loaded';
}
}
if (oXmlhttp.readyState==4 && oXmlhttp.status == 200) {
if (MSG != '') {
parent.document.getElementById (MSG).firstChild.data = 'OK';
}
parent.document.getElementById (TARGET).firstChild.appendData (oXmlhttp.responseText);
if (j < LIMIT) {
getFile (j);
}
}
}
oXmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var data = 'sType=FILE_LINE&iStart='+iStart+'&iStep='+STEP+'&sFile='+FILE;
oXmlhttp.send (data);
}
function getFilePat (iStart) {
var j = iStart;
oXmlhttp.open('POST', SCRIPT);
oXmlhttp.onreadystatechange=function() {
if (oXmlhttp.readyState==1) {
if (MSG != '') {
parent.document.getElementById (MSG).firstChild.data = 'Loading';
}
}
if (oXmlhttp.readyState==2) {
if (MSG != '') {
parent.document.getElementById (MSG).firstChild.data = 'Loaded';
}
}
if (oXmlhttp.readyState==4 && oXmlhttp.status == 200) {
if (MSG != '') {
parent.document.getElementById (MSG).firstChild.data = 'OK';
}
parent.document.getElementById (TARGET).firstChild.appendData (oXmlhttp.responseText);
j += oXmlhttp.responseText.length;
if (j < LIMIT) {
getFilePat (j);
}
}
}
oXmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var data = 'sType=FILE_PATTERN&iStart='+iStart+'&iStep='+STEP+'&sFile='+FILE;
oXmlhttp.send (data);
}
this.initialize = function (sType, mValue) {
switch (sType) {
case 'START' :
START = mValue;
return true;
break;
case 'LIMIT' :
LIMIT = mValue;
return true;
break;
case 'STEP' :
STEP = mValue;
return true;
break;
case 'FILE' :
FILE = mValue;
return true;
break;
case 'QUERY' :
QUERY = mValue;
return true;
break;
case 'TARGET' :
TARGET = mValue;
return true;
break;
case 'SCRIPT' :
SCRIPT = mValue;
return true;
break;
case 'MSG' :
MSG = mValue;
return true;
break;
default:
return false;
break;
}
}
this.getData = function (sType) {
switch (sType) {
case 'DEFAULT':
getDefault (START);
break;
case 'FILE_LINE':
getFile (START);
break;
case 'FILE_PATTERN':
getFilePat (START);
break;
case 'DB':
getDB (START);
break;
}
}
}
Merci par avance :-)