Mardzis
Mardzis

Reputation: 768

Why is not saved cookies in Javascript?

I have a website where I show a temporary message to the user. This message should behave as follows:

I need to store a cookie when an user clicks on the close button (.info-close) panel to update again after this panel does not show.

There is my problem on jsfiddle.

$(document).ready(function(){
    
    var $close = $("#info-panel .info-close");
    var $info_panel = $("#info-panel");
    var $site_main = $(".site-main");
    
    $close.click(function(){
        $info_panel.slideUp();
        $info_panel.addClass("closed");
        // set cookie
        document.cookie = "1";
        // save cookie to "$yes_closed"
        var $yes_closed = document.cookie;
    });
    
    $(window).scroll(function() {
        // if panel has class closed, after click on "X" > dont open
        // or if in cookie "$yes_closed" is save "1" > dont open
        if ($info_panel.hasClass("closed")) {
            $info_panel.hide();
        } else {
            if ($(this).scrollTop() > 0) {
                $info_panel.slideUp();
            } else {
                $info_panel.slideDown();
            }
        }
    
    });
    
});
#info-panel {
    padding: 4px 8px;
    background: yellow;
}

.info-close {
    padding: 5px;
    background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<br/><br/><br/>

<div id="info-panel">
		<div class="info-text">You are reading documentation for version 9.0.1. Documentation for earlier versions is available as pdfs here.</div>
		<div class="info-close">X</div>
	</div>

<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>


<p>"Hello my friend"</p>

Upvotes: 0

Views: 908

Answers (1)

ssbiswal1987
ssbiswal1987

Reputation: 96

It is because the cookie is not set properly. You need to add a cookie name and assign value to it. On page load do check if the cookie is set. Based on that show the message.

I have your code fixed here in the jsfiddle. Take a look.

http://jsfiddle.net/ssbiswal1987/9bhnyxwn/

Upvotes: 2

Related Questions