I have the need to maintain a text entered by the user (via form) for 30 days and that this text remains visible on every page of the site. I thought I’d handle it with cookies, but I did not succeed.
HTML
[code=html]<body onload=”verificaCookie()”>
<a href=”javascript:window.alert(document.cookie.split(‘;’).join(”))”>Tutti i cookies</a>
<div id=”div_form”>
<form id=”myform” method=”post” action=”#” onsubmit=”send()”>
<input id=”myinput” type=”text” name=”myinput” size=”20″ maxlength=”30″ value=”Messaggio…”/>
<input type=”button” value=”Send” onclick=”send()”/>
</div>
<div id=”msg”><p id=”mgs_view”></p></div>
</form>
</div>
JAVASCRIPT
[CODE]if(!navigator.cookieEnabled) {
alert(“Non hai i cookie abilitati.”);
}
function scriviCookie(nomeCookie,valoreCookie,durataCookie){
var scadenza = new Date();
var adesso = new Date();
scadenza.setTime(adesso.getTime() + (parseInt(durataCookie) * 60000));
document.cookie = nomeCookie + ‘=’ + escape(valoreCookie) + ‘; expires=’ + scadenza.toGMTString() + ‘; path=/’;
}
function leggiCookie(nomeCookie){
if (document.cookie.length > 0){
var inizio = document.cookie.indexOf(nomeCookie + “=”);
if (inizio != -1){
inizio = inizio + nomeCookie.length + 1;
var fine = document.cookie.indexOf(“;”,inizio);
if (fine == -1) fine = document.cookie.length;
return unescape(document.cookie.substring(inizio,fine));
}
else{
return “”;
}
}
return “”;}
function verificaCookie(){
var res = leggiCookie(“testo”);
if(res != “”){
document.getElementById(“div_form”).innerHTML = res + “”;
}
}
function send() {
var testo = document.getElementById(“myinput”).value;
scriviCookie(“testo”, testo, 30);
document.getElementById(“mgs_view”).innerHTML = testo;
}
Doing it this way does not work, yet the cookies are enabled. With Chrome just will not do, while Firefox is known that the cookie saves me but does not show me the text ..
I apologize for my poor english.