J'ai un programme qui envoie des lots de 3 variables dans un cookie, quand on clique sur un lien.
Les variables sont listées dans le cookie, puis à la page suivante de l'application web, elles sont lues et affichées dans un tableau.
Je voudrais empêcher le passage à la page suivante, si une valeur au moins parmi une certaine liste de valeurs, n'est pas lue dans le cookie.
Le lien qui envoie les valeurs est <a href="java script:buyItem('Produit','6.10','1')
Ensuite le programme qui les gère est celui-ci
CODE
function alterError(value) {
if (value<=0.99) {
newPounds = '0';
} else {
newPounds = parseInt(value);
}
newPence = parseInt((value+.0008 - newPounds)* 100);
if (eval(newPence) <= 9) newPence='0'+newPence;
newString = newPounds + '.' + newPence;
return (newString);
}
//-----------------------------------------------------------------------------------------------------------
function buyItem(newItem, newPrice, newQuantity) {
if (newQuantity <= 0) {
rc = alert('Entrez un quantité correcte !');
} else {
if (confirm('Voulez-vous ajouter : '+newQuantity+' fois "'+newItem+'" à votre panier ?')) {
index = document.cookie.indexOf("GSBasket");
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
if (countend == -1) {
countend = document.cookie.length;
}
document.cookie="GSBasket="+document.cookie.substring(countbegin, countend)+"["+newItem+"|"+newPrice+"|"+newQuantity+"]";
location.reload() ;
}
}
}
//-----------------------------------------------------------------------------------------------------------
function resetShoppingBasket() {
index = document.cookie.indexOf("GSBasket");
document.cookie="GSBasket=.";
if (document.cookie) {
} else {
alert('You have disabled using cookies.\nThis shop needs cookies to store your orders.\nPlease activate cookies in your browser settings and reload the shop pages.'); }
}
//-----------------------------------------------------------------------------------------------------------
function showItems() {
var tablerowcolor = '#E8BEFF';
index = document.cookie.indexOf("GSBasket");
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
if (countend == -1) {
countend = document.cookie.length;
}
fulllist = document.cookie.substring(countbegin, countend);
totprice = 0;
document.writeln('<FORM NAME="updateform">');
document.writeln('<div align="left">');
document.writeln('<table class="texte" WIDTH=100% BORDER=0 CELLPADDING="2">');
document.writeln('<TR BGCOLOR="#E8EBFF" ALIGN="left" VALIGN="top"><TD><FONT >Articles</FONT></TD><TD align="left"><FONT >Quantité</FONT></TD><TD align="left"><FONT >Prix par article</FONT></TD><td align="left"><FONT >Total</FONT></TD><TD> </TD></TR>');
// Nun die horizontale Linie:
document.writeln('<TR BGCOLOR="C8E8FD"><TD colspan="5"><HR></TD></TR>');
itemlist = 0;
for (var i = 0; i <= fulllist.length; i++) {
if (fulllist.substring(i,i+1) == '[') {
itemstart = i+1;
thisitem = 1;
} else if (fulllist.substring(i,i+1) == ']') {
itemend = i;
thequantity = fulllist.substring(itemstart, itemend);
itemtotal = 0;
itemtotal = (eval(theprice*thequantity));
temptotal = itemtotal * 100;
totprice = totprice + itemtotal;
itemlist=itemlist+1;
document.write('<tr valign="center" BGCOLOR="'+tablerowcolor+'"><td><FONT >'+theitem+'</FONT></td>');
document.write('<td valign="center" align="left"><INPUT TYPE=TEXT NAME="quant'+itemlist+'" VALUE="'+thequantity+'" SIZE=1></td><td valign="center" align="left"><FONT >'+theprice+'</FONT></td><td valign="center" align="left"><FONT >'+alterError(itemtotal)+'</FONT></td><td align="left" valign="top"><a href="java script:removeItem('+itemlist+')"><FONT >Effacer</font></a><br><a href="java script:amendItem('+itemlist+',document.updateform.quant'+itemlist+'.value)"><FONT >Calculer</font></a></td></tr>');
if (tablerowcolor == '#E8BEFF') {
tablerowcolor = "#C8E8FD";
} else {
tablerowcolor = "#E8BEFF"; }
} else if (fulllist.substring(i,i+1) == '|') {
if (thisitem==1) theitem = fulllist.substring(itemstart, i);
if (thisitem==2) theprice = fulllist.substring(itemstart, i);
thisitem++;
itemstart=i+1; }
}
document.writeln('<TR BGCOLOR="FCFCFC"><TD colspan="5"><HR></TD></TR>');
document.writeln('<tr BGCOLOR="#E8EBFF" ALIGN="left" VALIGN="top"><td colspan=3><FONT >Total :</font> </td><td align=left><FONT >'+alterError(totprice)+' Euros</FONT></td><td> </td></tr>');
document.writeln('</TABLE><cr>');
document.writeln('</div>');
document.writeln('</FORM>');
}
//-----------------------------------------------------------------------------------------------------------
function amendItem(itemno, newquant) {
newItemList = null;
itemlist = 0;
for (var i = 0; i <= fulllist.length; i++) {
if (fulllist.substring(i,i+1) == '[') {
thisitem = 1;
itemstart = i+1;
fullstart = i+1;
} else if (fulllist.substring(i,i+1) == ']') {
itemend = i;
itemlist=itemlist+1;
if (itemlist != itemno) {
newItemList = newItemList+'['+fulllist.substring(fullstart, itemend)+']';
} else {
newItemList = newItemList + '['+theitem+'|'+theprice+'|'+newquant+']'; }
} else if (fulllist.substring(i,i+1) == '|') {
if (thisitem==1) theitem = fulllist.substring(itemstart, i);
if (thisitem==2) theprice = fulllist.substring(itemstart, i);
thisitem++;
itemstart=i+1; }
}
index = document.cookie.indexOf("GSBasket");
document.cookie="GSBasket="+newItemList;
self.location = "fdp.html";
}
//-----------------------------------------------------------------------------------------------------------
function removeItem(itemno) {
newItemList = null;
itemlist = 0;
for (var i = 0; i <= fulllist.length; i++) {
if (fulllist.substring(i,i+1) == '[') {
itemstart = i+1;
} else if (fulllist.substring(i,i+1) == ']') {
itemend = i;
theitem = fulllist.substring(itemstart, itemend);
itemlist=itemlist+1;
if (itemlist != itemno) {
newItemList = newItemList+'['+fulllist.substring(itemstart, itemend)+']'; }
}
}
index = document.cookie.indexOf("GSBasket");
document.cookie="GSBasket="+newItemList;
self.location = "fdp.html";
}
//-----------------------------------------------------------------------------------------------------------
// clearBasket() - removes all items from the basket
function clearBasket() {
if (confirm('Voulez vous vider votre panier ?')) {
index = document.cookie.indexOf("GSBasket");
document.cookie="GSBasket=.";
self.location = "fdp.html"; }
}
//----------------------------------------------------------------------------------------------------------- ICI : la fonction de test qui m'intéresse;
function lit_cook() {
if (theitem !="France")
{
rc = alert('Entrez vos frais de port');
}
else {
self.location='commander.html';
}
}
if (value<=0.99) {
newPounds = '0';
} else {
newPounds = parseInt(value);
}
newPence = parseInt((value+.0008 - newPounds)* 100);
if (eval(newPence) <= 9) newPence='0'+newPence;
newString = newPounds + '.' + newPence;
return (newString);
}
//-----------------------------------------------------------------------------------------------------------
function buyItem(newItem, newPrice, newQuantity) {
if (newQuantity <= 0) {
rc = alert('Entrez un quantité correcte !');
} else {
if (confirm('Voulez-vous ajouter : '+newQuantity+' fois "'+newItem+'" à votre panier ?')) {
index = document.cookie.indexOf("GSBasket");
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
if (countend == -1) {
countend = document.cookie.length;
}
document.cookie="GSBasket="+document.cookie.substring(countbegin, countend)+"["+newItem+"|"+newPrice+"|"+newQuantity+"]";
location.reload() ;
}
}
}
//-----------------------------------------------------------------------------------------------------------
function resetShoppingBasket() {
index = document.cookie.indexOf("GSBasket");
document.cookie="GSBasket=.";
if (document.cookie) {
} else {
alert('You have disabled using cookies.\nThis shop needs cookies to store your orders.\nPlease activate cookies in your browser settings and reload the shop pages.'); }
}
//-----------------------------------------------------------------------------------------------------------
function showItems() {
var tablerowcolor = '#E8BEFF';
index = document.cookie.indexOf("GSBasket");
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
if (countend == -1) {
countend = document.cookie.length;
}
fulllist = document.cookie.substring(countbegin, countend);
totprice = 0;
document.writeln('<FORM NAME="updateform">');
document.writeln('<div align="left">');
document.writeln('<table class="texte" WIDTH=100% BORDER=0 CELLPADDING="2">');
document.writeln('<TR BGCOLOR="#E8EBFF" ALIGN="left" VALIGN="top"><TD><FONT >Articles</FONT></TD><TD align="left"><FONT >Quantité</FONT></TD><TD align="left"><FONT >Prix par article</FONT></TD><td align="left"><FONT >Total</FONT></TD><TD> </TD></TR>');
// Nun die horizontale Linie:
document.writeln('<TR BGCOLOR="C8E8FD"><TD colspan="5"><HR></TD></TR>');
itemlist = 0;
for (var i = 0; i <= fulllist.length; i++) {
if (fulllist.substring(i,i+1) == '[') {
itemstart = i+1;
thisitem = 1;
} else if (fulllist.substring(i,i+1) == ']') {
itemend = i;
thequantity = fulllist.substring(itemstart, itemend);
itemtotal = 0;
itemtotal = (eval(theprice*thequantity));
temptotal = itemtotal * 100;
totprice = totprice + itemtotal;
itemlist=itemlist+1;
document.write('<tr valign="center" BGCOLOR="'+tablerowcolor+'"><td><FONT >'+theitem+'</FONT></td>');
document.write('<td valign="center" align="left"><INPUT TYPE=TEXT NAME="quant'+itemlist+'" VALUE="'+thequantity+'" SIZE=1></td><td valign="center" align="left"><FONT >'+theprice+'</FONT></td><td valign="center" align="left"><FONT >'+alterError(itemtotal)+'</FONT></td><td align="left" valign="top"><a href="java script:removeItem('+itemlist+')"><FONT >Effacer</font></a><br><a href="java script:amendItem('+itemlist+',document.updateform.quant'+itemlist+'.value)"><FONT >Calculer</font></a></td></tr>');
if (tablerowcolor == '#E8BEFF') {
tablerowcolor = "#C8E8FD";
} else {
tablerowcolor = "#E8BEFF"; }
} else if (fulllist.substring(i,i+1) == '|') {
if (thisitem==1) theitem = fulllist.substring(itemstart, i);
if (thisitem==2) theprice = fulllist.substring(itemstart, i);
thisitem++;
itemstart=i+1; }
}
document.writeln('<TR BGCOLOR="FCFCFC"><TD colspan="5"><HR></TD></TR>');
document.writeln('<tr BGCOLOR="#E8EBFF" ALIGN="left" VALIGN="top"><td colspan=3><FONT >Total :</font> </td><td align=left><FONT >'+alterError(totprice)+' Euros</FONT></td><td> </td></tr>');
document.writeln('</TABLE><cr>');
document.writeln('</div>');
document.writeln('</FORM>');
}
//-----------------------------------------------------------------------------------------------------------
function amendItem(itemno, newquant) {
newItemList = null;
itemlist = 0;
for (var i = 0; i <= fulllist.length; i++) {
if (fulllist.substring(i,i+1) == '[') {
thisitem = 1;
itemstart = i+1;
fullstart = i+1;
} else if (fulllist.substring(i,i+1) == ']') {
itemend = i;
itemlist=itemlist+1;
if (itemlist != itemno) {
newItemList = newItemList+'['+fulllist.substring(fullstart, itemend)+']';
} else {
newItemList = newItemList + '['+theitem+'|'+theprice+'|'+newquant+']'; }
} else if (fulllist.substring(i,i+1) == '|') {
if (thisitem==1) theitem = fulllist.substring(itemstart, i);
if (thisitem==2) theprice = fulllist.substring(itemstart, i);
thisitem++;
itemstart=i+1; }
}
index = document.cookie.indexOf("GSBasket");
document.cookie="GSBasket="+newItemList;
self.location = "fdp.html";
}
//-----------------------------------------------------------------------------------------------------------
function removeItem(itemno) {
newItemList = null;
itemlist = 0;
for (var i = 0; i <= fulllist.length; i++) {
if (fulllist.substring(i,i+1) == '[') {
itemstart = i+1;
} else if (fulllist.substring(i,i+1) == ']') {
itemend = i;
theitem = fulllist.substring(itemstart, itemend);
itemlist=itemlist+1;
if (itemlist != itemno) {
newItemList = newItemList+'['+fulllist.substring(itemstart, itemend)+']'; }
}
}
index = document.cookie.indexOf("GSBasket");
document.cookie="GSBasket="+newItemList;
self.location = "fdp.html";
}
//-----------------------------------------------------------------------------------------------------------
// clearBasket() - removes all items from the basket
function clearBasket() {
if (confirm('Voulez vous vider votre panier ?')) {
index = document.cookie.indexOf("GSBasket");
document.cookie="GSBasket=.";
self.location = "fdp.html"; }
}
//----------------------------------------------------------------------------------------------------------- ICI : la fonction de test qui m'intéresse;
function lit_cook() {
if (theitem !="France")
{
rc = alert('Entrez vos frais de port');
}
else {
self.location='commander.html';
}
}
---------------------------------------------------------
Voici par ailleurs les liens qui contiennent les variables à tester
<a href="java script:buyItem('France','1.00','1')">Ajouter à votre panier France</a>
<a href="java script:buyItem('DOM','2.00','1')">Ajouter à votre panier DOM</a>
--------------------------------
J'ai donc deux liens ou plus sur la page, et je voudrais que le test se fasse sur chacun des liens. Je n'arrive pas à trouver la solution. J'ai testé plusieurs syntaxes avec ||, mais je n'y arrive pas. dès que je mets un autre code, mon tableau ne s'affiche carrément plus dans la page de résultat.
Pour l'instant, cela marche si on ne clique pas sur le lien qui contient la variable "France". Cela marche aussi si on ne clique sur rien. Cela veut sire que si on clique sur le lien DOM, ou sur aucun lien, le passage à la page suivante est bien bloqué.
Mais je voudrais que le test se fasse pour savoir si au moins une variable de la liste de liens est trouvée dans le cookie, avant d'autoriser le passage à la page suivante.
Je ne sais pas si c'est très clair.
Merci de votre aide.