每次用户在网站上时只显示一次div [英] Show a div only once per time the user is on the site
问题描述
我试图在用户每次登录网站时只显示一次div元素。我已经看到类似问题的多个答案,但他们都没有工作......我写了一个代码,但它不工作......我从其他地方得到了一部分代码(我可以不记得),为什么它不起作用?
以下是代码
< script type =text / javascript>
//<![CDATA [
var once_per_session = 1
function get_cookie(Name){
var search = Name +=
var returnvalue =;
if(document.cookie.length> 0){
offset = document.cookie.indexOf(search)
if(offset!= -1){//如果cookie存在
offset + = search.length
//设置值开始索引
end = document.cookie.indexOf(;,offset);
//设置cookie值结束索引
if(end == -1)
end = document.cookie.length;
returnvalue = unescape(document.cookie.substring(offset,end))
}
}
return returnvalue;
函数alertornot(){
if(get_cookie('alerts')==''){
loadalert()
document.cookie =alerts = yes
}
}
函数loadalert(){
document.getElementById(popupp)。style.visibility = visible;
if(once_per_session == 0)
loadalert()
else
alertornot()
//]]>
< / script>
< div class =popuppstyle =visibility:hidden;> hi< / div>
你认为你可以帮我弄清楚它有什么问题吗?
编辑
我懂了!这里是:
< script type ='text / javascript'> //<![CDATA [
window.onload = function(){
(function(){
var visited = localStorage.getItem('visited');
if(!visited){
document .getElementById(popupp)。style.visibility =visible;
localStorage.setItem('visited',true);
}
})();
} //]]>
< / script>
< / head>
< body>
< div id =popuppstyle =visibility:hidden;> hi< / div>
< / body>
您可以改用localStorage,只有当用户清除它会再次弹出(与cookie相同),但它更简单:
(function(){
访问= localStorage.getItem('visited');
if(!visited){
document.getElementById(popupp)。style.visibility =visible;
localStorage。 setItem('visited',true);
}
})();
html:
< div id =popuppstyle =visibility:hidden;> hi< / div>
通过这种方式,您可以获得更少的代码。希望这有助于。
I'm trying to show a div element only once per time that the user is on the site. I've seen multiple answers to similar questions already, but none of them seem to work... I have a code written out, but it doesn't work... I got a part of the code from somewhere else (I can't remember) so is that why it doesn't work?
Here's the code
<script type="text/javascript">
//<![CDATA[
var once_per_session=1
function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1)
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}
function alertornot(){
if (get_cookie('alerted')==''){
loadalert()
document.cookie="alerted=yes"
}
}
function loadalert(){
document.getElementById("popupp").style.visibility = visible;
if (once_per_session==0)
loadalert()
else
alertornot()
//]]>
</script>
<div class="popupp" style="visibility:hidden;">hi</div>
Do you think you could help me figure out what's wrong with it?
EDIT
I've got it! Here it is:
<script type='text/javascript'>//<![CDATA[
window.onload=function(){
(function() {
var visited = localStorage.getItem('visited');
if (!visited) {
document.getElementById("popupp").style.visibility = "visible";
localStorage.setItem('visited', true);
}
})();
}//]]>
</script>
</head>
<body>
<div id="popupp" style="visibility:hidden;">hi</div>
</body>
You could use localStorage instead, only when the user clears it out he will get the popup again (same with cookies) but its much simplier:
(function() {
var visited = localStorage.getItem('visited');
if (!visited) {
document.getElementById("popupp").style.visibility = "visible";
localStorage.setItem('visited', true);
}
})();
html:
<div id="popupp" style="visibility:hidden;">hi</div>
This way you get a lot of less code. Hope this helps.
这篇关于每次用户在网站上时只显示一次div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!