cookie控制窗口打開打開

今日寫了個用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="../images/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>
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章