admin管理员组

文章数量:1418019

I want to implement on my site show-hidden div block as on stackoverflow - at a time when the user wants to hide it himself putted on button "X". May already have a ready-made solution? I am not verse in Javascript and would be very grateful for the help!

Picture:

I want to implement on my site show-hidden div block as on stackoverflow. - at a time when the user wants to hide it himself putted on button "X". May already have a ready-made solution? I am not verse in Javascript and would be very grateful for the help!

Picture:

Share Improve this question edited Dec 17, 2013 at 11:17 ProgramFOX 6,39011 gold badges48 silver badges54 bronze badges asked Dec 17, 2011 at 18:07 user1103744user1103744 2,4614 gold badges21 silver badges20 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

This is simple working example without using any external library. You can improve it with your animation/design. Also these functions for cookies can be very simplier but you can use it in future to set not only boolean value. UPDATE: I added functon for show your popup again (for debug mostly).

<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();">
     <div id="popup" style="display:none">Hello! Wele to my site. If you want to hide this message then click <a href="#" onclick="hidePopup(); return false;">[x]</a></div>
     <div>Some static text here.</div>
     <div>Bring me <a href="#" onclick="showPopup(); return false;">back</a> my popup!</div>
</body>
</html>

Alternative:

  • Using localStorage instead of cookie
  • LocalStorage never expire until they delete personal data on browser
  • jQuery Library for easy customization on future
  • re-display hidden remembered content
  • jsfiddle/axcelleria/41t76s8q/

For mobile detection: jsfiddle/axcelleria/nmosxbgm/

本文标签: Hide block (and remember it) with JavaScript and cookiesStack Overflow