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 badges2 Answers
Reset to default 4This 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
版权声明:本文标题:Hide block (and remember it) with JavaScript and cookies? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745282256a2651489.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论