www.webdeveloper.com
Results 1 to 4 of 4

Thread: Problem with cookies

  1. #1
    Join Date
    Nov 2013
    Posts
    1

    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
    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>
            <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.

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

  3. #3
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Chrome won't create cookies under the file:// protocol.

    Your code seems to work under Firefox.

    Code:
      scadenza.setTime(adesso.getTime() + (parseInt(durataCookie) * 60000));
    Can be coded more clearly as:
    Code:
        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
    Location
    localhost
    Posts
    2,204
    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.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

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



Recent Articles