Sample Code used to determine whether the client browser supports cookies
This article describes the sample code used to determine whether the client browser supports cookies. For more information, see.
1.
The Code is as follows:
Function check (){
If (window. navigator. cookieEnabled)
Return true;
Else {
Alert ("browser configuration error, Cookie unavailable! ");
Return false ;}
}
2.
The Code is as follows:
SetCookie ('cookie _ test', '1 ');
Var cookie_test = getCookie ('cookie _ test ');
If ('1 '! = Cookie_test)
{
Alert ('unsupported ');
} Else
{
Alert ('support ');
}
Function SetCookie (name, value) // two parameters: one is the name of the cookie and the other is the value.
{
Var Days = 30; // This cookie will be saved for 30 Days
Var exp = new Date (); // new Date ("December 31,999 8 ");
Exp. setTime (exp. getTime () + Days x 24x60*60*1000 );
Document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();
}
Function getCookie (name) // The cookie function.
{
Var arr = document. cookie. match (new RegExp ("(^ |)" + name + "= ([^;] *) (; | $ )"));
If (arr! = Null) return unescape (arr [2]); return null;
}
Function delCookie (name) // delete a cookie
{
Var exp = new Date ();
Exp. setTime (exp. getTime ()-1 );
Var cval = getCookie (name );
If (cval! = Null) document. cookie = name + "=" + cval + "; expires =" + exp. toGMTString ();
}
3.
The Code is as follows:
Var cookieEnabled = (navigator. cookieEnabled )? True: false
// Determine whether the cookie is enabled
// If the browser is not ie4 + or ns6 +
If (typeof navigator. cookieEnabled = "undefined "&&! CookieEnabled ){
Document. cookie = "testcookie"
CookieEnabled = (document. cookie = "testcookie ")? True: false
Document. cookie = "" // erase dummy value
}