js的一些常用方法,保存cookie等

来源:互联网 发布:淘宝直通车开车条件 编辑:程序博客网 时间:2024/04/26 11:46

分享你一个在工作能用到的js方法

// 添加Cookiefunction 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" : "");    }}// 获取Cookiefunction getCookie(name) {    if (name != null) {        var value = new RegExp("(?:^|; )" + encodeURIComponent(String(name)) + "=([^;]*)").exec(document.cookie);        return value ? decodeURIComponent(value[1]) : null;    }}// 移除Cookiefunction removeCookie(name, options) {    addCookie(name, null, options);}//js模拟StringBufferfunction 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;        }    }
1 0
原创粉丝点击