Home > Web Front-end > JS Tutorial > body text

Sample code to determine whether the customer's browser supports cookies_javascript skills

WBOY
Release: 2016-05-16 17:07:50
Original
1228 people have browsed it

1.

Copy code The code is as follows:

function check(){
if( window.navigator.cookieEnabled)
return true;
else{
alert("Browser configuration error, Cookie is not available!");
return false;}
}

2.
Copy code The code is as follows:

SetCookie('cookie_test' ,'1');
var cookie_test = getCookie('cookie_test');
if( '1' != cookie_test)
{
alert('not supported');
} 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, 9998");
exp.setTime( exp.getTime() Days*24*60*60*1000);
document.cookie = name "=" escape (value) ";expires=" exp.toGMTString();
}
function getCookie(name)//Get 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();
}

3.
Copy code The code is as follows:

var cookieEnabled=(navigator.cookieEnabled)? true : false
//Determine whether cookies are turned on

//If browsing The server 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
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template