Results 1 to 4 of 4

Thread: Problem with cookies

  1. #1
    Join Date
    Nov 2013

    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:
    <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 id="msg"><p id="mgs_view"></p></div>
    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));
         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.

  2. #2
    Join Date
    Nov 2013
    Have you tried another browser incase firefox has cached data etc?

  3. #3
    Join Date
    Mar 2007
    Chrome won't create cookies under the file:// protocol.

    Your code seems to work under Firefox.

      scadenza.setTime(adesso.getTime() + (parseInt(durataCookie) * 60000));
    Can be coded more clearly as:
        scadenza.setDate( scadenza.getDate() +  durataCookie );
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  4. #4
    Join Date
    Mar 2007
    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.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center