1

I have one promo header that span if click on X. Right now when I refresh the page the span comes back. I need cookies to remember 24h that span is closed.

This is what I tried with no results:

Html code:

<div id=”span” class="promo_holder">
    <a href="javascript:void(0)" id=”close” class="close_promo_header" 
       onclick="hidePromoPanel ('/');">Close<span>X</span></a>
</div>

JavaScript code:

$(document).ready(function() {


    if (!readCookie('hide')) {
        $('#span').show();
    }


    $('#close').click(function() {
        $('#span').hide();
        createCookie('hide', true, 1)
        return false;
    });

});


function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}  

function eraseCookie(name) {
    createCookie(name,"",-1);
}
Blazer
  • 306
  • 4
  • 16
  • If you run this `readCookie('hide')` in console, what do you see? – dfsq Mar 16 '13 at 18:43
  • What exactly does not work? Inspect the cookie values with the devtools. And where did you get those cookie-function from, are they self-coded or copied from a reliable source? – Bergi Mar 16 '13 at 18:44
  • I never did it. I don't know how to run script on console. – Blazer Mar 16 '13 at 18:47
  • Press F12, select "Console". You can run there any javascript code in context of the page. – dfsq Mar 16 '13 at 18:49
  • My code is the first html code. Second code is copied from here http://stackoverflow.com/questions/10674611/hide-div-24hr-cookie-javascript and I have changed id. – Blazer Mar 16 '13 at 18:50
  • When press F12 /console and pasted readCookie('hide') I receive ReferenceError: readCookie is not defined – Blazer Mar 16 '13 at 18:52
  • Are you sure your script is included in the page at all? – Bergi Mar 16 '13 at 18:56
  • Anyway, script itself seems to be ok http://jsfiddle.net/Nd4xb/ – dfsq Mar 16 '13 at 18:57
  • Sorry, it wasn't, I forgot I delete it after I sow no result. I pasted back , I get result on console "true" – Blazer Mar 16 '13 at 19:00
  • Check here please http://testare.besaba.com/ Cookies does not remember that the span was closed and after refresh its open again. – Blazer Mar 16 '13 at 19:17
  • Do I need any javascript plugin or why doesn't work on my test site ? – Blazer Mar 16 '13 at 20:07

0 Answers0