今日写了个用cookie控制弹出窗口的小程序,记录下来,没准以后能用到

cookie.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
       <title>My JSP 'cookie.jsp' starting page</title>
  </head>

  <script type="text/javascript">

//获取cookie的值
    function getCookie(name){
             var strCookie=document.cookie;
             var arrCookie=strCookie.split(";");
             for(var i=0;i<arrCookie.length;i++){
             var arr=arrCookie[i].split("=");
            if(arr[0]==name){
                        return arr[1];
        }
     }
    return "";
    }

 /**

这也是一个取得cookie值的方法但是较复杂

function 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 "";
    }

**/
  function ready(name)
       if(getCookie(name)!="no"){  //如果cookie为name的值不为no便新开一个窗口
          window.open('new.jsp','notice','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no, resizable=no,width=300,height=355');
       }
 </script>
  <body onload="ready("notice")">
 
    这是测试cookie的页面
  </body>
</html>
 

2、new.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>弹出窗口</title>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">
<LINK HREF="../css/skstyle.css" REL="stylesheet" TYPE="text/css">
</head>

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


function closeWin()
{

 if ( document.forms[0].Notice.checked ){//如果选中设置一个cookie将值设为no,过期时间为一天,即只是今天不显示该窗口,此处cookie值与main.jsp中的值相对应
 setCookie( "Notice", "no" , 1);
 self.close();
}

</script>

<body topmargin="0" leftmargin="0">
<form>
<table width="300" border="0" cellspacing="0" cellpadding="0">
 
  <tr>
    <td height="30" bgcolor="#86E8F2"><table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td align="right"><input type="checkbox" name="Notice" value="">
            今天一天不显示</td>
          <td width="4" align="right">&nbsp;</td>
          <td width="66" align="right"><img src="../p_w_picpaths/btn/btn_close.gif" width="66" height="22" style="cursor:hand;" onClick="javascript:closeWin();"></td>
          <td width="4">&nbsp;</td>
        </tr>
      </table></td>
  </tr>
</table>
</form>
</body>
</html>