Reputation: 69
I'm a serious newbie in JS, and I have this function that changes div style from none to block and reverse, and I need it to set a cookie and remember state, so when I navigate off the page and return it hides or shows the div as previously selected. Can anyone help me with that?
This is the simple code I have for now:
function toggle(id) {
var e = document.getElementById(id);
e.style.display = ((e.style.display!='none') ? 'none' : 'block');
}
How do i make it save and use the cookie in browsers? Thanks.
Upvotes: 0
Views: 1167
Reputation: 1562
you should set the cookie when user leaves the page This done in body tag's "onunload" event. Like:
onunload="setCookie(c_name,value,exdays)";
c_name is a name for your cookie. Such as "divVisibility". value is your cookie's value, such as "visible". exdays is the number of days your cookie will expire in.
on page load, check the value of cookie with getCookie(c_name). Set div's visibility according its value.
Functions i told to use and information about cookies: http://www.w3schools.com/js/js_cookies.asp
Upvotes: 1