Javascript共通方法汇总

将平时项目中用到的js共通方法汇总于此,方便以后的使用(除ztree扩展外均已测试通过):

  • cookie操作
  1. 添加cookie;
  2. 获取cookie;
  3. 删除cookie;
  4. 清除所有cookie;
  • url操作
  1. 取得url中参数;
  • string操作
  1. 对字符串进行base64编码;
  2. 对字符串进行base64解码;
  3. 对字符串进行UTF-8编码(返回UTF-8编码字符串);
  4. 对UTF-8编码字符串进行解码(返回UTF-8解码后的字符串);
  • ztree扩展
  1. 根据属性ID勾选对应的节点;
  2. 去除包含node的array中重复的所有子节点(根据【tId】判重);
  • 对象操作
  1. 深度复制(支持:字符串、array、object、date);
  2. 获取对象类型;

代码:

  • cookie操作
function CookieUtil(){
	// 添加cookie (有参: key, value, time(单位:秒), 无返)
	this.addCookie=function(key,value,time){
		if (typeof time == "number") {
			//数字类型
			document.cookie = key + "=" + value + ";path=/;max-age=" + time;
		}else if (typeof time == "string") {
			//字符串类型
			document.cookie = key + "=" + value + ";path=/;expires=" + time;
		}else {
			//对象类型 先toString 后expires
			document.cookie = key + "=" + value + ";path=/;expires=" + time.toString();
		}
	}

	// 获取cookie (有参: key, 有返:key对应的value)
	this.getValue=function(key){
		var arr1 = document.cookie.split("; ")
		for (var i = 0 ; i < arr1.length; i++) {
			var arr2 = arr1[i].split("=")
			if (arr2[0] == key) {
				return arr2[1];
			}
		}
	}

	// 删除cookie (有参:key , 无返)
	this.deleteCookie=function(key){
		document.cookie = key + "=;max-age=-1";
	}

	// 清除所有cookie函数
	this.clearAllCookie=function(){
		var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
		if(keys) {
			for(var i = keys.length; i--;){
				this.deleteCookie(keys[i]);
			}
		}
	}
}
  • url操作
function UrlUtil(){
	/**
	 * 取得url中参数
	 * @param name 参数名
	 * @return 参数值
	 */
	this.getUrlParam=function(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = window.location.search.substr(1).match(reg);
		if (r != null) return unescape(decodeURI(r[2])); 
		return null;
	}
}
  • string操作
function StringUtil(){
	this._base64KeyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	/**
	 * 对字符串进行base64编码
	 */
	this.base64Encode=function(input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
		input = this.utf8Encode(input);
		while (i < input.length) {
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
			output = output +
			this._base64KeyStr.charAt(enc1) + this._base64KeyStr.charAt(enc2) +
			this._base64KeyStr.charAt(enc3) + this._base64KeyStr.charAt(enc4);
		}
		return output;
	}

	/**
	 * 对字符串进行base64解码
	 */
	this.base64Decode=function(input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
		while (i < input.length) {
			enc1 = this._base64KeyStr.indexOf(input.charAt(i++));
			enc2 = this._base64KeyStr.indexOf(input.charAt(i++));
			enc3 = this._base64KeyStr.indexOf(input.charAt(i++));
			enc4 = this._base64KeyStr.indexOf(input.charAt(i++));
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
			output = output + String.fromCharCode(chr1);
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
		}
		output = this.utf8Decode(output);
		return output;
	}

	/**
	 * 对字符串进行UTF-8编码(返回UTF-8编码字符串)
	 */
	this.utf8Encode=function(string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			} else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			} else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	}

	/**
	 * 对UTF-8编码字符串进行解码(返回UTF-8解码后的字符串)
	 */
	this.utf8Decode=function(utftext) {
		var string = "";
		var i = 0;
		var c = 0;
		var c1 = 0;
		var c2 = 0;
		var c3 = 0;
		while ( i < utftext.length ) {
			c = utftext.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			} else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			} else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
}
  • ztree扩展
function ZTreeUtil(){
	/**
	 * 根据属性ID勾选对应的节点
	 * @param treeObj 指定节点所属的treeObj
	 * @param id 属性ID值
	 * @param checked、checkTypeFlag、callbackFlag 参照ztree官方文档中【checkNode】的说明
	 */
	this.checkZtreeNodeById = function(treeObj,id,checked,checkTypeFlag,callbackFlag){
		var nodeById = treeObj.getNodeByParam("id",id);
		if (nodeById != null && nodeById != undefined) {
			treeObj.checkNode(nodeById, checked, checkTypeFlag,callbackFlag);
		}
	}

	/**
	 * 去除节点array中重复的所有子节点
	 * @param nodes 要处理的节点array
	 */
	this.removeRepeatChildren = function(nodes){
		var tempNodes = [];
		tempNodes = tempNodes.concat(nodes);
		// 防止查询出重复的结果
		for (var pNodeIndex in tempNodes) {
			for (var cNodeIndex in nodes) {
				// 去除所有子节点
				if (nodes[cNodeIndex].parentTId == tempNodes[pNodeIndex].tId) {
					nodes.splice(cNodeIndex,1,"REPEAT_"+cNodeIndex);
				}
			}
		}
		
		tempNodes = [];
		tempNodes = tempNodes.concat(nodes);
		// 清空且不更换数组
		nodes.length = 0;
		for (var index in tempNodes) {
			if (typeof tempNodes[index] != "string") {
				nodes.push(tempNodes[index]);
			}
		}
	}
}
  • 对象操作
function ObjectUtil(){
	/**
	 * 获取对象的副本(深度复制)
	 * @param {Object} data 支持string、array、object、date
	 * @return 深度复制副本,若为不支持的类型则返回原始对象
	 */
	this.deepCopy=function(data) {
		const t = this.typeOf(data);
		// 初始化返回值
		var o;
		if (t === 'array') {
			o = [];
		} else if ( t === 'object') {
			o = {};
		} else if (t === 'date') {
			o = new Date();
		} else {
			return data;
		}
		// 复制
		if (t === 'array') {
			for (let i = 0; i < data.length; i++) {
				o.push(this.deepCopy(data[i]));
			}
		} else if ( t === 'object') {
			for (let i in data) {
				o[i] = this.deepCopy(data[i]);
			}
		} else if (t === 'date') {
			o.setTime(data.getTime());
		}
		return o;
	}

	/**
	 * 获取对象类型
	 * @param {Object} obj 指定对象
	 */
	this.typeOf = function(obj) {
		const toString = Object.prototype.toString;
		const map = {
			'[object Boolean]'  : 'boolean',
			'[object Number]'   : 'number',
			'[object String]'   : 'string',
			'[object Function]' : 'function',
			'[object Array]'    : 'array',
			'[object Date]'     : 'date',
			'[object RegExp]'   : 'regExp',
			'[object Undefined]': 'undefined',
			'[object Null]'     : 'null',
			'[object Object]'   : 'object'
		};
		return map[toString.call(obj)];
	}
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值