zl程序教程

您现在的位置是:首页 >  后端

当前栏目

js弹出框只弹一次(二次修改之后的)

JS 修改 一次 之后 弹出 二次
2023-06-13 09:15:12 时间
弹出框只弹一次,看到网上也就写的很多,但真正能拿过来用的没有几个。以下是我修改之后的代码,供参考。

这段代码是使用了cookie来控制的,首先使用cookie让浏览器记住这个页面已经打开过一次,如果再次引用这个页面已经打开一次了,如果再次引用这个页面的话将不进行打开。而浏览器一旦关闭浏览器,保存这个记录的cookie文件将被删除。因此关闭浏览器,再次打开的话弹出窗口还会出现的,从而确保了在原有的窗口基础上只打开一个窗口。
复制代码代码如下:

<scripttype="text/javascript">
varreturnvalue="";
functionopenpopup(){
<s:iftest="isAlreadyGetGift==0">
$.colorbox({inline:true,href:"#getGiftForm",innerWidth:"650px;",innerHeight:"475px;",onOpen:true});
$("#getGiftSuccess").hide();
</s:if>
}
functionget_cookie(Name){
varsearch=Name+"=";
if(document.cookie.length>0){
offset=document.cookie.indexOf(search);
if(offset!=-1){
//ifcookieexists
offset+=search.length;
//setindexofbeginningofvalue
end=document.cookie.indexOf(";",offset);
//setindexofendofcookievalue
if(end==10){
end=document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset,end));
}
}
}
returnreturnvalue;
}
functionloadpopup(){
if(get_cookie("popped")==""){
openpopup();
document.cookie="popped=yes"
}
}
$(document).ready(function(){

loadpopup();

});
</script>