/ / Write cookies function Author:翟振凯
function SetCookie (name, value) / / two parameters, one is the cookie name, a value are
(
var Days = 30; / / This cookie will be saved for 30 days
var exp = new Date (); / / new Date ( "December 31, 9998");
exp.setTime (exp.getTime () + Days * 24 * 60 * 60 * 1000);
document.cookie = name + "=" + escape (value) + "; expires =" + exp.toGMTString ();
)
function getCookie (name) / / check cookies function
(
var arr = document.cookie.match (new RegExp ("(^|) "+ name +"=([^;]*)(;|$)"));
if (arr! = null) return unescape (arr [2]); return null;
)
function delCookie (name) / / delete cookie
(
var exp = new Date ();
exp.setTime (exp.getTime () - 1);
var cval = getCookie (name);
if (cval! = null) document.cookie = name + "=" + cval + "; expires =" + exp.toGMTString ();
)
SetCookie ( "xiaoqi", "3")
alert (getCookie ( 'xiaoqi'));
</ script>
A very useful javascript function to read and write Cookie
A very useful javascript function to read and write Cookie
function GetCookieVal (offset)
/ / Get decoded value Cookie
(
var endstr = documents.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = documents.cookie.length;
return unescape (documents.cookie.substring (offset, endstr));
)
function SetCookie (name, value)
/ / Set Cookie Value
(
var expdate = new Date ();
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc> 2)? argv [2]: null;
var path = (argc> 3)? argv [3]: null;
var domain = (argc> 4)? argv [4]: null;
var secure = (argc> 5)? argv [5]: false;
if (expires! = null) expdate.setTime (expdate.getTime () + (expires * 1000));
documents.cookie = name + "=" + escape (value) + ((expires == null)? "": ( "; expires =" + expdate.toGMTString ()))
+ ((path == null)? "": ( "; path =" + path)) + ((domain == null)? "": ( "; domain =" + domain))
+ ((secure == true)? "; secure": "");
)
function DelCookie (name)
/ / Delete Cookie
(
var exp = new Date ();
exp.setTime (exp.getTime () - 1);
var cval = GetCookie (name);
documents.cookie = name + "=" + cval + "; expires =" + exp.toGMTString ();
)
function GetCookie (name)
/ / Get the original value of Cookie
(
var arg = name + "=";
var alen = arg.length;
var clen = documents.cookie.length;
var i = 0;
while (i <clen)
(
var j = i + alen;
if (documents.cookie.substring (i, j) == arg)
return GetCookieVal (j);
i = documents.cookie.indexOf ( "", i) + 1;
if (i == 0) break;
)
return null;
)
<SCRIPT Language="javascript">
<! --
function openpopup () (
url = "popup.htm"
window.open ( "gonggao.htm", "gonggao", "width = 260, height = 212, left = 200, top = 0")
)
function get_cookie (Name) (
var search = Name + "="
var returnvalue = "";
if (documents.cookie.length> 0) (
offset = documents.cookie.indexOf (search)
if (offset! = -1) (
offset + = search.length
end = documents.cookie.indexOf (";", offset);
if (end == -1)
end = documents.cookie.length;
returnvalue = unescape (documents.cookie.substring (offset, end))
)
)
return returnvalue;
)
function helpor_net () (
if (get_cookie ( 'popped')==''){
openpopup ()
documents.cookie = "popped = yes"
)
)
helpor_net ()
//-->
</ SCRIPT>
To determine if the point, as long as the clear cookie, after the visit will not be prompted again, if we do not point to determine the prompt every time. Js file on the whole station contains
<SCRIPT LANGUAGE="JavaScript">
<! --
var the_cookie = document.cookie;
var broken_cookie = the_cookie.split (":");
var the_visiteraccepted = unescape (broken_cookie [1]);
/ /
if (the_visiteraccepted == "undefined") (
var tmp = confirm ( 'Chinese people when and where.');
if (tmp == false) (
window.close ();
) else (
var the_visiteraccepted = 1;
var the_cookie = "ILoveChina = visiteraccepted:" + escape (the_visiteraccepted);
document.cookie = the_cookie;
)
)
//-->
</ SCRIPT>
1. Cookie compatibility question
Cookie format has two different versions, the first version, we call Cookie Version 0, was initially developed by Netscape Corporation, is also almost all browsers support. The newer version, Cookie Version 1, is based on RFC 2109 document drawn up. In order to ensure compatibility, JAVA provisions mentioned above, the operation involved Cookie are directed at the older version of the Cookie conducted. The new version of the Cookie is not being supported by Javax.servlet.http.Cookie package.
2. Cookie content
Cookie contents of the same character restrictions for different versions have different Cookie. Cookie Version 0 at certain special characters, such as: space, in square brackets, parentheses, equal sign (=), comma, double quotes, slashes, question marks, the @ symbol, a colon, a semicolon can not be as Cookie content. That is why we have set an example for Cookie content "Test_Content" reasons.
While Cookie Version 1 provisions relaxed restrictions, you can use these characters, but taking into account the new version of the Cookie is still not standardized for all browsers support it, which was for insurance purposes, we should as far as possible the content of Cookie avoid using these characters
http://www.hq-poly.com/news/read.asp?id=137







