js的一些常用方法,保存cookie等
2016-01-07 13:57
501 查看
分享你一个在工作能用到的js方法
// 添加Cookie function addCookie(name, value, options) { if (arguments.length > 1 && name != null) { if (options == null) { options = {}; } if (value == null) { options.expires = -1; } if (typeof options.expires == "number") { var time = options.expires; var expires = options.expires = new Date(); expires.setTime(expires.getTime() + time * 1000); } document.cookie = encodeURIComponent(String(name)) + "=" + encodeURIComponent(String(value)) + (options.expires ? "; expires=" + options.expires.toUTCString() : "") + (options.path ? "; path=" + options.path : "") + (options.domain ? "; domain=" + options.domain : ""), (options.secure ? "; secure" : ""); } } // 获取Cookie function getCookie(name) { if (name != null) { var value = new RegExp("(?:^|; )" + encodeURIComponent(String(name)) + "=([^;]*)").exec(document.cookie); return value ? decodeURIComponent(value[1]) : null; } } // 移除Cookie function removeCookie(name, options) { addCookie(name, null, options); } //js模拟StringBuffer function StringBuffer() { this.__strings__ = new Array; } StringBuffer.prototype.append = function (str) { this.__strings__.push(str); }; StringBuffer.prototype.toString = function () { return this.__strings__.join(""); }; String.prototype.replaceAll = function(s1,s2){ return this.replace(new RegExp(s1,"gm"),s2); } //去空取值 function getStringNoNull(s){ if(s==null || s==undefined) { return ""; }else{ return s; } }
相关文章推荐
- Json的所有转化
- JS实现复制到剪贴板功能
- javascript数据类型边边角角
- JSON
- js判断手机的类型
- D3.js-数值自动变动的条形图表
- Javascript面向对象编程(三):非构造函数的继承
- Javascript面向对象编程(二):构造函数的继承
- Javascript面向对象编程(一):封装
- js根据浏览器窗口大小实时改变网页文字大小
- ajax json 动态传值
- JS获取上下左右键的keycode值
- JS之页面自动跳转
- JavaScript进阶(八)JS实现图片预览并导入服务器功能
- [jstips]向数组中插入一个元素
- EXTJS grid columnprogressbar进度条
- EXTJS表格列xtype详解
- D3.js-博客园使用框架
- js 选项卡
- js变量,json格式编码困惑