10 examples of 'how to set cookie in jquery' in JavaScript

Every line of 'how to set cookie in jquery' 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
52function setCookie(key, value, exp, path, domain) {
53 if (!(typeof key === 'string' && key.length)) {
54 return; // Key is mandatory
55 }
56 if (typeof value !== 'string') {
57 value = '';
58 } //If value is invalid by default empty string will be set
59 var dt = new Date();
60 if (typeof exp === 'number') {
61 if (exp === Infinity) {
62 dt = new Date('Thu, 31 Dec 2037 00:00:00 GMT');
63 } else {
64 dt.setTime(dt.getTime() + exp * 24 * 60 * 60 * 1000);
65 }
66 }
67 var expires = exp ? '; expires=' + dt.toUTCString() : '',
68 cookiePath = '; path=' + (typeof path === 'string' ? path.trim() : '/'),
69 defaultDomain = window.location.hostname,
70 cookieDomain = '';
71 if (defaultDomain === 'localhost') {
72 // IE does not allow localhost domain
73 if (typeof domain === 'string') {
74 cookieDomain = '; domain=' + domain.trim();
75 }
76 } else {
77 cookieDomain = '; domain=' + (typeof domain === 'string' ? domain.trim() : defaultDomain);
78 }
79
80 var secureCookieFlag = '';
81 if (location.protocol === 'https:') {
82 secureCookieFlag = '; secure';
83 }
84 document.cookie = key + '=' + value + expires + cookieDomain + cookiePath + secureCookieFlag;
85}
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

51function set_cookie() {
52 var date=new Date();
53 date.setTime(date.getTime()+7*24*3600*1000);
54 document.cookie="cross-cookie=disabled;expires="+date.toGMTString();
55 location.reload();
56}
80function setCookie(cookie, value){
81 var expires = new Date();
82 expires.setTime(expires.getTime() + 62208000000); //1000*60*60*24*30*24 (2 years)
83 document.cookie = cookie + "=" + value + "; expires=" + expires.toGMTString() + "; domain=" + domain + "; path=/";
84}
152function setcookie(){
153 console.log('设置的键值对:',val.value)
154 cookie(JSON.parse(val.value))
155 inof.innerHTML = '设置成功!' + val.value;
156}
353function storeCookie(nome, valore) {
354 var d = new Date();
355 d.setTime(d.getTime() + (24 * 60 * 60 * 1000));
356 var expires = "expires=" + d.toUTCString();
357 document.cookie = nome + "=" + valore + "; " + expires + "; path=/";
358}
21function setCookie (c_name, value, expiredays) {
22 var exdate = new Date();
23 exdate.setDate(exdate.getDate() + expiredays);
24 document.cookie = c_name + '=' + escape(value) + ((expiredays == null) ? '' : ';expires=' + exdate.toGMTString())+ '; path='+window.nps.web_base_url+'/;';
25}
31function createCookie(c, d, e) {
32 var a = '';
33 if (e) {
34 var b = new Date();
35 b.setTime(b.getTime() + (e * 1000));
36 a = '; expires=' + b.toGMTString();
37 }
38
39 document.cookie = c + '=' + d + a + ';domain=.gfycat.com;path=/';
40}
63function SetSessionCookie(cookieName, cookieValue, path, domain) {
64 document.cookie = cookieName+"="+escape(cookieValue) + ";path="+path+";domain="+domain;
65}
10function setPageCookie(name, value) {
11 document.cookie = name + "=" + escape(value) + "; path=" + getPath();
12}
10function setCookie (name, value, days) {
11 let expires = '';
12 if (days) {
13 const date = new Date();
14 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
15 expires = '; expires=' + date.toUTCString();
16 }
17 document.cookie = name + '=' + (value || '') + expires + '; path=/';
18}

Related snippets