2014-09-04 79 views
0

我得到這個JS腳本,設置cookie和顯示/隱藏DIVJS餅乾div可見

<html> 
<head> 
<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 hidePopup() { 
    setCookie('popup_state', false); 
    document.getElementById('popup').style.display = 'none'; 
} 
function showPopup() { 
    setCookie('popup_state', null); 
    document.getElementById('popup').style.display = 'block'; 
} 
function checkPopup() { 
    if (getCookie('popup_state') == null) { // if popup was not closed 
     document.getElementById('popup').style.display = 'block'; 
    } 
} 

</script> 
</head> 
<body onLoad="checkPopup();"> 
<a href="#" onClick="hidePopup(); return false;">Turn Off</a></br></br> 
<a href="#" onClick="showPopup(); return false;">Turn On</a> 
    <div id="popup" style="display:none">Hello! Welcome to my site. If you want to hide this message then click</div> 
</body> 
</html> 

我需要有人,如果有人好心地向我解釋,我怎麼能更改cookie值? (名稱,過期等)以及如何將開/關按鈕合併到1按鈕中,以完成切換顯示/隱藏和相應編輯cookie的工作。

回答

1

更改cookie的當前值可以用thesame名稱,將重疊的cookie當前值

你的第二個問題,你可以使用複選框重新設置cookie的開/關

function shoWhidePopup(el){ 
    if(el.checked==true){ 
     showPopup(); 
    }else if(el.checked==false){ 
     hidePopup(); 
    } 
} 

,並在HTML

<label> 
    <input type="checkbox" onclick="shoWhidePopup(this);" hidden> 
    <span style="appearance:button; -moz-appearance:button; -webkit-appearance:button;">show/hide</span> 
</label> 
+0

這樣的作品,但更需要它作爲一個按鈕,然後一個複選框。可以這樣做嗎? – Ethernity4ever 2014-09-04 02:48:17