原生JS设置、删除、获取Cookie
<script> /* * 设定Cookie值 */ function setCookie(cookieName, value, msToExpire, path, domain, isSecure) { var expiryDate; // 相对到期时间(以毫秒为单位) if (msToExpire) { expiryDate = new Date(); expiryDate.setTime(expiryDate.getTime() + msToExpire); } document.cookie = cookieName + '=' + window.encodeURIComponent(value) + (msToExpire ? ';expires=' + expiryDate.toGMTString() : '') + ';path=' + (path || '/') + (domain ? ';domain=' + domain : '') + (isSecure ? ';secure' : ''); } /* * 获取Cookie值 */ function getCookie(cookieName) { var cookiePattern = new RegExp('(^|;)[ ]*' + cookieName + '=([^;]*)'), cookieMatch = cookiePattern.exec(document.cookie); return cookieMatch ? window.decodeURIComponent(cookieMatch[2]) : 0; } /* * 删除Cookie值 */ function deleteCookie(cookieName, path, domain) { setCookie(cookieName, '', -86400, path, domain); } <script>
文章来源地址https://www.toymoban.com/diary/js/41.html
文章来源:https://www.toymoban.com/diary/js/41.html
到此这篇关于原生JS设置、删除、获取Cookie的文章就介绍到这了,更多相关内容可以在右上角搜索或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!