자바스크립트 하루동안 안 열리는 팝업
<html>
<head>
<script>
function window.onload(){ //페이지 로드 시 팝업 띄움.
var cookie= getCookie("CookieName");
if (cookie!= "noPopup") { //창 닫기를 체크하지 않았으면 팝업 띄움.
var url = "popup.htm";
window.open(url, null, "width=500, height=500");
}
}
function getCookie(name) { //쿠키 가져오기
var Found = false;
var start, end;
var i = 0;
while(i <= document.cookie.length) {
start = i
end = start + name.length
if(document.cookie.substring(start, end) == name) {
Found = true;
break;
}
i++;
}
if(Found == true) {
start = end + 1;
end = document.cookie.indexOf(";", start);
if(end < start) {
end = document.cookie.length;
}
return document.cookie.substring(start, end)
}
return "";
}
</script>
</head>
<body>
팝업 띄울 페이지
</body>
</html>
다음은 팝업 페이지.
<html>
<head>
<script>
function setCookie( name, value, expiredays ) { //쿠키 설정
var todayDate = new Date();
todayDate.setDate( todayDate.getDate() + expiredays );
document.cookie = name + "=" + escape( value )
+ "; path=/; expires=" + todayDate.toGMTString() + ";"
}
function closeWindow() {
var check = document.getElementById("event"); //event라는 이름의 체크박스를 가져옴.
if (check.checked) { //체크박스가 선택되어 있으면
setCookie("CookieName", "noPopup" , 1); //쿠키 이름을 'noPopup'으로 설정.
}
self.close(); //팝업창을 닫는다.
}
</script>
</head>
<body>
<input type="checkbox" name="event" onclick="javascript:closeWindow();"/>닫기
</body>
</html>