javascript - Setting a cookie to remember hidden element state -


i'm having trouble setting cookie remember whether elements have been hidden or not. script i'm using (which came answer on here) seems work in terms of hiding or showing elements i'm trying control, doesn't appear working in terms of 'remembering' current state when reloading/changing pages.

on site i'm developing, 2 pages have code:

http://www.emelisandetribute.com/music.php , http://www.emelisandetribute.com/videos.php

the javascript used on pages (directly before tag) follows:

<script type="text/javascript"> function setcookie (name, value, expires, path, domain, secure) {     document.cookie = name + "=" + escape(value) +     ((expires) ? "; expires=" + expires : "") +     ((path) ? "; path=" + path : "") +     ((domain) ? "; domain=" + domain : "") +     ((secure) ? "; secure" : ""); } function getcookie (name) {     var cookie = " " + document.cookie;     var search = " " + name + "=";     var setstr = null;     var offset = 0;     var end = 0;     if (cookie.length > 0) {         offset = cookie.indexof(search);         if (offset != -1) {             offset += search.length;             end = cookie.indexof(";", offset);             if (end == -1) {                 end = cookie.length;             }             setstr = unescape(cookie.substring(offset, end));         }     }     if (setstr == 'false') {         setstr = false;     }      if (setstr == 'true') {         setstr = true;     }     if (setstr == 'null') {         setstr = null;     }     return(setstr); } function hidefooter() {     setcookie('footer_state', false);      document.getelementbyid('toggleoff').style.display = 'none';     document.getelementbyid('toggleon').style.display = 'block';     document.getelementbyid('footer1').style.display = 'none';     document.getelementbyid('footer2').style.display = 'none';     document.getelementbyid('footer3').style.display = 'none'; } function showfooter() {     setcookie('footer_state', null);     document.getelementbyid('toggleoff').style.display = 'block';     document.getelementbyid('toggleon').style.display = 'none';     document.getelementbyid('footer1').style.display = 'block';     document.getelementbyid('footer2').style.display = 'block';     document.getelementbyid('footer3').style.display = 'block'; } function checkfooter() {     if (getcookie('footer_state') == null) { // if popup not closed         document.getelementbyid('toggleoff').style.display = 'block';         document.getelementbyid('toggleon').style.display = 'none';         document.getelementbyid('footer1').style.display = 'block';         document.getelementbyid('footer2').style.display = 'block';         document.getelementbyid('footer3').style.display = 'block';     }    } </script> 

the html relevant elements is:

<div class="mainfooterwrapper">     <span class="mainfootertoggle" id="toggleoff"><a href="#" onclick="hidefooter(); return false;">hide this</a></span>     <span class="mainfootertoggle" id="toggleon" style="display:none"><a href="#" onclick="showfooter(); return false;">show this</a></span>     <div class="mainfooter1" id="footer1">         <?php include "mainfooter1.php"; ?>     </div>     <div class="mainfooter2" id="footer2">     <?php include "mainfooter4.php"; ?>     </div>     <div class="mainfooter3" id="footer3">     <?php include "mainfooter3.php"; ?>         <div style="clear:both; float:right; position:absolute; bottom:0; right:0; overflow:hidden; margin:5px 15px 5px 5px;">         <?php include "footer.php"; ?>         </div>     </div> </div> 

from reading of other questions , answers, tried adding of elements, both editing css , adding code html, made elements disappear completely.

it crossed mind need piece of code create cookie when elements hidden user, delete/amend cookie if elements shown again. realise have @ moment (although not working properly) code has 'on' switch, no 'off' switch.

any or advice appreciated.

what missing calling code set hide footer if hidden. never this. checkfooter function obvious 1 this. don't use right now, should. should reverse logic hides footer if getcookie('footer_state') == false. change function this:

function checkfooter() {     if (getcookie('footer_state') == false) { // if popup closed         document.getelementbyid('toggleoff').style.display = 'none';         document.getelementbyid('toggleon').style.display = 'block';         document.getelementbyid('footer1').style.display = 'none';         document.getelementbyid('footer2').style.display = 'none';         document.getelementbyid('footer3').style.display = 'none';     }    } 

now make sure call method when page loads. add following code @ bottom of page google analytics , mcustomscrollbar scripts:

<script>     checkfooter(); </script> 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -