Skip to content

Instantly share code, notes, and snippets.

@wangwen1220
Last active December 24, 2018 11:20
Show Gist options
  • Save wangwen1220/8429263 to your computer and use it in GitHub Desktop.
Save wangwen1220/8429263 to your computer and use it in GitHub Desktop.
js: 原生 JavaScript 设置 cookie 值 | setCookie
// 原生 JavaScript 设置 cookie 值
function setCookie(name, value, time) {
var now = new Date();
var offset = 8;
var utc = now.getTime() + (now.getTimezoneOffset() * 60000);
var nd = utc + (3600000 * offset);
var exp = new Date(nd);
var domain = document.domain;
exp.setTime(exp.getTime() + time * 60 * 60 * 1000);
document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString() + ";domain=" + domain + ";"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment