/    Sign up×
Community /Pin to ProfileBookmark

Problem with cookies

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>[/code]

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;
}[/CODE]

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.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@iC0D3Nov 16.2013 — Have you tried another browser incase firefox has cached data etc?
Copy linkTweet thisAlerts:
@Logic_AliNov 17.2013 — Chrome won't create cookies under the file:// protocol.

Your code seems to work under Firefox.

[CODE] scadenza.setTime(adesso.getTime() + (parseInt(durataCookie) * 60000));[/CODE][/QUOTE]Can be coded more clearly as:
[CODE] scadenza.setDate( scadenza.getDate() + durataCookie );[/CODE]
Copy linkTweet thisAlerts:
@rootNov 17.2013 — Suggest you install a development server like XAMPP or if you are feeling adventurous you could install something like Abyss and PHP and use that, in either case a server on your PC that you can start and stop as you need it will be a big benefit.
×

Success!

Help @stefano861 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...