salve, vorrei utilizzare un semplice carrello in js che utilizza i cookies.
crea un cookie del tipo:
document.cookie = 'Lampade=#@'+codice+'@'+prezzo+'@'+quant+'##';
il problema è che se creo il cookie nella cartella www.dominio.it/banane/ il cookie vale solo per quella cartella.
dunque quando apro il file carrello.htm è sempre vuoto perchè questo si trova nella root.
la domanda è: nello script che posto qui sotto come faccio a lavorare con un cookie che valga per la root e ttte le sottocartelle?
grazie
qui ci sono gli articoli
questo è il carrellocodice:<script language="javascript"> var numprod=0; function vedicarrello() { window.open('carrello.htm','Popup_Window','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=760,height=350'); } function primavolta(codice, prezzo, quant) { document.cookie = 'Lampade=#@'+codice+'@'+prezzo+'@'+quant+'##'; } function cancella() { document.cookie = "Lampade=0"; } function aggiungi2(codice, prezzo, quant) { var sost=0; var sostflag=0; var subpuntatore=0; var string=document.cookie; var puntatore=string.lastIndexOf("Lampade=#"); var lung=string.indexOf("##"); if (puntatore == -1) primavolta(codice, prezzo, quant); else { var newstring="Lampade=#"; while (puntatore<=lung) { sost=0; if (string.charAt(puntatore) == "@") { puntatore++; subpuntatore=puntatore; while (string.charAt(puntatore) != "@") puntatore++; if (string.substring(subpuntatore, puntatore) == codice) sost=1; newstring+="@"+string.substring(subpuntatore, puntatore); puntatore++; subpuntatore=puntatore; while (string.charAt(puntatore) != "@") puntatore++; newstring+="@"+string.substring(subpuntatore, puntatore); puntatore++; subpuntatore=puntatore; while ((string.charAt(puntatore) != "@") && (string.charAt(puntatore) != "#")) puntatore++; if (sost == 1) { var somma=parseInt(quant); somma+=parseInt(string.substring(subpuntatore, puntatore)); if (somma > 999) somma=999; newstring+="@"+somma; sost=0; sostflag=1; } else newstring+="@"+string.substring(subpuntatore, puntatore); puntatore--; } puntatore++; } if (sostflag == 1) newstring+="##"; else newstring=string.substring(0,lung)+"@"+codice+"@"+prezzo+"@"+quant+"##"; } document.cookie=newstring; } function aggiungi(codice, prezzo) { var quant=prompt("Inserire la quantità che si desidera aquistare",1); if ((isNaN(quant)) || (quant<1) || (quant>999)) alert("Valore non valido"); else { if (document.cookie.length > 0) aggiungi2(codice, prezzo, quant); else primavolta(codice, prezzo, quant); } } </script> Aggiungi
codice:<script language="javascript"> function deleteart(nomearticolo) { var deletearticle=0; var sostflag=0; var subpuntatore=0; var string=document.cookie; var puntatore=string.lastIndexOf("Lampade=#"); var lung=string.indexOf("##"); var newstring="Lampade=#"; while (puntatore<=lung) { deletearticle=0; if (string.charAt(puntatore) == "@") { puntatore++; subpuntatore=puntatore; while (string.charAt(puntatore) != "@") puntatore++; if (string.substring(subpuntatore, puntatore) == nomearticolo) deletearticle=1; if (deletearticle==0) newstring+="@"+string.substring(subpuntatore, puntatore); puntatore++; subpuntatore=puntatore; while (string.charAt(puntatore) != "@") puntatore++; if (deletearticle==0) newstring+="@"+string.substring(subpuntatore, puntatore); puntatore++; subpuntatore=puntatore; while ((string.charAt(puntatore) != "@") && (string.charAt(puntatore) != "#"))puntatore++; if (deletearticle==0) newstring+="@"+string.substring(subpuntatore, puntatore); puntatore--; } puntatore++; } newstring+="##"; puntatore=newstring.lastIndexOf("@"); if (puntatore != -1) document.cookie=newstring; else document.cookie = "Lampade=0"; location.href="carrello.htm"; } function chiudi() { close(); } function punteggiatura(num) { retval =""+num; if (retval < 100) { retval="0,"+retval; while (retval.length < 10){ retval =" "+retval } } if ((retval <= 99999) && (retval >= 100)) { temp1 = retval.substring(0,retval.length-2); temp2 = retval.substring(retval.length-2,retval.length+1); retval =temp1+","+temp2; while (retval.length < 10){ retval =" "+retval } } if(retval >= 100000) { temp1 = retval.substring(0,retval.length-5); temp2 = retval.substring(retval.length-5,retval.length-2); temp3 = retval.substring(retval.length-2,retval.length+1); retval=temp1+"."+temp2+","+temp3; while (retval.length < 10){ retval =" "+retval } } return retval; } document.write('<div align="center"> <center>'); document.write("Contenuto del carrello: "); var quant=0; var prezzo=0; var totale=0; var string=document.cookie; var puntatore=string.lastIndexOf("Lampade=#"); var lung=string.indexOf("##"); if (puntatore == -1) document.write(' Il carrello è vuoto'); else { document.write('<table border="1" cellspacing="1">'); document.write('<tr><td><font face="Verdana, Arial, Helvetica, sans-serif">Articolo</font></td>'); document.write('<td align="right"><font face="Verdana, Arial, Helvetica, sans-serif">Prezzo</font></td>'); document.write('<td align="right"><font face="Verdana, Arial, Helvetica, sans-serif">Quantità</font></td>'); document.write('<td align="right"><font face="Verdana, Arial, Helvetica, sans-serif">Totale</font></td>'); document.write('<td align="right"></td></tr>'); while (puntatore<=lung) { if (string.charAt(puntatore) == "@") { document.write('<tr>'); puntatore++; subpuntatore=puntatore; while (string.charAt(puntatore) != "@") puntatore++; nomearticolo=string.substring(subpuntatore, puntatore); document.write('<td align="left"><font face="Verdana, Arial, Helvetica, sans-serif"> '+string.substring(subpuntatore, puntatore)+'</font></td>'); puntatore++; subpuntatore=puntatore; while (string.charAt(puntatore) != "@") puntatore++; prezzo=parseFloat(string.substring(subpuntatore, puntatore)); document.write('<td align="right"><font face="Verdana, Arial, Helvetica, sans-serif">€'+punteggiatura(parseInt(prezzo*100))+'</font></td>'); puntatore++; subpuntatore=puntatore; while ((string.charAt(puntatore) != "@") && (string.charAt(puntatore) != "#")) puntatore++; quant=parseInt(string.substring(subpuntatore, puntatore)); document.write('<td align="right"><font face="Verdana, Arial, Helvetica, sans-serif">'+quant+'</font></td>'); totale+=parseInt(prezzo*quant*100); document.write('<td align="right"><font face="Verdana, Arial, Helvetica, sans-serif">€'+punteggiatura(parseInt(prezzo*quant*100))+'</font></td>'); document.write('<td align="right"><font face="Verdana, Arial, Helvetica, sans-serif">Cancella</font></td>'); puntatore--; document.write('</tr>'); } puntatore++; } document.write('<tr>'); document.write('<td colspan="3" align="right"></td>'); document.write('<td align="right"><font face="Verdana, Arial, Helvetica, sans-serif">Totale: €'+punteggiatura(totale)+'</font></td>'); document.write('</tr> </table> </center> </div>'); } document.write('<p align="center"><input type="button" value="Chiudi" name="B3" onClick="chiudi()"></p>'); </script>

Rispondi quotando