컴퓨터이야기/Language2009. 4. 27. 22:55

[자바스크립트]하루동안 팝업창열지않기

먼저 팝업창에 하루동안 열지않기 라는 체크박스와 함께
사용자가 체크박스에 체크를했는지를 저장할 쿠키를 만들어줄 소스를 입력해야한다.

팝업창안의 내용

<head>
<script language="JavaScript">
<!--
function notice_setCookie( name, value, expiredays )
    {
        var todayDate = new Date();
        todayDate.setDate( todayDate.getDate() + expiredays );
        document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";"
        }

function notice_closeWin()
{
        if ( document.forms[0].Notice.checked )
                notice_setCookie( "Notice", "done" , 1); // 1=하룻동안 공지창 열지 않음
        self.close();
}
function na_call(str){  eval(str);}
//-->
</script>
</head>
<body>
<form name="form1">
<input type="checkbox" name="Notice"  OnClick="notice_closeWin()">오늘은 이창을 다시 열지않음
</form>
</body>


흔히 이걸로 끝이라고 생각하지만 오산이다
팝업창을 열어주는 페이지에서도
팝업을 불러올때 notice_setCookie 사용자함수로 저장된 쿠키를 읽어와서 하루가지났는지 체크를 한후
쿠키가 남아있다면 팝업을 열지않고
쿠키가 남아있지않다면 팝업을 열어주어야한다.


<script language="JavaScript">
<!--
function notice_getCookie( name ){
    var nameOfCookie = name + "=";
    var x = 0;
    while ( x <= document.cookie.length )
    {
            var y = (x+nameOfCookie.length);
            if ( document.cookie.substring( x, y ) == nameOfCookie ) {
                    if ( (endOfCookie=document.cookie.indexOf( ";", y )) == -1 )
                            endOfCookie = document.cookie.length;
                    return unescape( document.cookie.substring( y, endOfCookie ) );
            }
            x = document.cookie.indexOf( " ", x ) + 1;
            if ( x == 0 )
                    break;
    }
    return "";
}
if ( notice_getCookie( "Notice" ) != "done" )
{
        window.open('팝업창주소','','left=5,top=200,width=525,height=425'); // 팝업윈도우의 경로와 크기를 설정 하세요
}

// -->
</script>

이렇게 하면 된다.



레이어팝업은 다음이시간에...

Posted by iloje