10 examples of 'delete a cookie in javascript' in JavaScript

Every line of 'delete a cookie in javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
84function delCookie (NameOfCookie) {
85 if (getCookie(NameOfCookie)) {
86 document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
87 }
88}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

1function deleteCookie ()
2 {
3 var cookies = document.cookie.split(";");
4
5 for (var i = 0; i < cookies.length; i++) {
6 var cookie = cookies[i];
7 var eqPos = cookie.indexOf("=");
8 var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
9 document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
10 }
11 window.location = "login.php"
12}
41_deleteCookie(name, domain, callback) {
42 // phantomJS completely ignores the domain parameter
43 // and deletes cookies only based on their names
44 try {
45 // try-catch just in case...
46 var ret = phantom.deleteCookie(name)
47 } catch(e) {
48 callback(`deleteCookie: failed to delete cookie "${cookie.name}": ${e.toString()}`)
49 return
50 }
51 if (ret) {
52 callback(null)
53 } else {
54 callback(`deleteCookie: failed to delete cookie "${cookie.name}"`)
55 }
56}
55function deleteCookie(name, path, domain)
56{
57 if (getCookie(name))
58 {
59 document.cookie = name + "=" +
60 ((path) ? "; path=" + path : "") +
61 ((domain) ? "; domain=" + domain : "") +
62 "; expires=Thu, 01-Jan-70 00:00:01 GMT";
63 }
64}
38function deleteCookie (name) {
39 setCookie(name, '', -1)
40}
32static remove(cookie) {
33 let date = new Date( );
34 date.setTime(date.getTime( ) - 3600 * 3600);
35
36 document.cookie = this._prefix + "_" + cookie + "= ; " + date.toUTCString( ) + "; path=/";
37}
9function delCookie({name, domain, path}) {
10 var date = new Date();
11 date.setTime(date.getTime() - 1);
12 document.cookie = [
13 encodeURIComponent(name) , '=',
14 domain && '; domain=' + domain,
15 path && '; path=' + path,
16 '; expires=', date.toUTCString()
17 ].join('');
18}
336function removeCookie(name){var expire = new Date((new Date()).getTime() - 1000); setCookie(name,'',null,expire,'/',null);}
45function _delCookie( name )
46{
47 _setCookie( name, '', ( new Date() - 86400000 ) );
48}
7function removeCookie(name) {
8 // 设置已过期,系统会立刻删除cookie
9 setCookie(name, '1', -1);
10}

Related snippets