分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

以JQuery的方式封装 cookie 方便调用

发布时间:2023-09-06 01:14责任编辑:苏小强关键词:暂无标签

一.封装

jQuery.cookie = function (key, value, options) { ???// key and value given, set cookie... ???if (arguments.length > 1 && (value === null || typeof value !== "object")) { ???????options = jQuery.extend({}, options); ???????if (value === null) { ???????????options.expires = -1; ???????} ???????if (typeof options.expires === ‘number‘) { ???????????var days = options.expires, t = options.expires = new Date(); ???????????t.setDate(t.getDate() + days); ???????} ???????return (document.cookie = [ ???????????encodeURIComponent(key), ‘=‘, ???????????options.raw ? String(value) : encodeURIComponent(String(value)), ???????????options.expires ? ‘; expires=‘ + options.expires.toUTCString() : ‘‘, // use expires attribute, max-age is not supported by IE ???????????options.path ? ‘; path=‘ + options.path : ‘‘, ???????????options.domain ? ‘; domain=‘ + options.domain : ‘‘, ???????????options.secure ? ‘; secure‘ : ‘‘ ???????].join(‘‘)); ???}

二.调用

//存cookiejQuery.cookie("username","admin",{ expires:7 });jQuery.cookie("password","123456",{ expires:7 });//取cookievar username = $.cookie("username");

expires:7设置过期时间为7天,-1为关闭浏览器后失效

三.备注

JQuery的版本为1.6.2。以上为个人笔记,可能不具备参考性。

本文连接:http://www.cnblogs.com/CryOnMyShoulder/p/7603255.html

以JQuery的方式封装 cookie 方便调用

原文地址:http://www.cnblogs.com/CryOnMyShoulder/p/7603255.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved