本節(jié)內(nèi)容:
js實(shí)現(xiàn)彈出框只彈一次。
以下代碼使用了cookie來控制的,首先使用cookie讓瀏覽器記住這個(gè)頁面已經(jīng)打開過一次,如果再次引用這個(gè)頁面已經(jīng)打開一次了,如果再次引用這個(gè)頁面的話將不進(jìn)行打開。
而瀏覽器一旦關(guān)閉瀏覽器,保存這個(gè)記錄的cookie文件將被刪除。
因此,關(guān)閉瀏覽器,再次打開的話彈出窗口還會出現(xiàn)的,從而確保了在原有的窗口基礎(chǔ)上只打開一個(gè)窗口。
例子:
<script type="text/javascript">
/**
* 彈出框只彈一次
* edit:www.jquerycn.cn
*/
var returnvalue = "";
function openpopup(){
<s:if test="isAlreadyGetGift == 0">
$.colorbox({inline:true, href:'#getGiftForm',innerWidth:'650px;',innerHeight:'475px;',onOpen:true});
$("#getGiftSuccess").hide();
</s:if>
}
function get_cookie(Name) {
var search = Name + "=";
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 == 10){
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end));
}
}
}
return returnvalue;
}
function loadpopup(){
if (get_cookie("popped")==""){
openpopup();
document.cookie="popped=yes"
}
}
$(document).ready(function(){
loadpopup();
});
</script>