/**
* Created by gaojun-pd on 2016/10/27.
*/
var Util = {
/**
* 1、判断非空
* 2、获取字符串真实长度 汉字算两位
* 3、判断参数类型
* 4、日期格式化
* 5、通过key获取url中的参数值
* 6、设置cookie值
* 7、获取cookie值
* 8、删除cookie
* 9、HTML编码
* 10、HTML解码
* 11、光标停在文字的后面,文本框获得焦点时调用
* 12、生成一个新的GUID
*/
/**
* 判断非空
* @param obj
* @returns {boolean}
*/
isEmpty: function (obj) {
if (obj == undefined || obj == null || new String(obj).trim() == '') {
return true;
} else {
return false;
}
}, /**
* 获取字符串真实长度 汉字算两位
* @param str
* @returns {number}
*/
getRealLength: function (str) {
return isEmpty(str) ? 0 : str.replace(/[^\x00-\xff]/g, "**").length;
}, /**
* 判断参数类型
* @param obj
* @returns {string}
*/
type: function (obj) {
var class2type = {},
toString = Object.prototype.toString;
(function () {
var typeArr = "Boolean,Number,String,Function,Array,Date,RegExp,Object".split(",");
for (var i = 0; i < typeArr.length; i++) {
var name = typeArr[i];
class2type["[object " + name + "]"] = name.toLowerCase();
}
})()
return obj == null ? String(obj) : class2type[toString.call(obj)] || "object";
}, /**
* 日期格式化
* @param date 日期对象
* @param formatStr 格式化字符串 如YYYY-MM-dd hh:mm:ss
* @returns {*}
*/
dateFormat: function (date, formatStr) {
var str = formatStr;
var Week = ['日', '一', '二', '三', '四', '五', '六'];
str = str.replace(/yyyy|YYYY/, this.getFullYear());
str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
str = str.replace(/MM/, (this.getMonth() + 1) > 9 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1));
str = str.replace(/M/g, (this.getMonth() + 1));
str = str.replace(/w|W/g, Week[this.getDay()]);
str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
str = str.replace(/d|D/g, this.getDate());
str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
str = str.replace(/h|H/g, this.getHours());
str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
str = str.replace(/m/g, this.getMinutes());
str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
str = str.replace(/s|S/g, this.getSeconds());
return str
}, /**
* 通过key获取url中的参数值
* @param key
* @returns {null}
*/
getQueryString: function (key) {
var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return decodeURIComponent(r[2]);
return null;
}, /**
* 设置cookie值
* @param name 名称
* @param value 名称对应值
* @param Hours 过期时间
*/
setCookie: function (name, value, Hours) {
var d = new Date();
var offset = 8;
var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
var nd = utc + (3600000 * offset);
var exp = new Date(nd);
exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000);
document.cookie = name + "=" + encodeURIComponent(value) + ";path=/;expires=" + exp.toGMTString() + ";domain=sicd.com;";
}, /**
* 获取cookie值
* @param name cookie名
* @returns {*}
*/
getCookie: function (name) {
var arr = document.cookie
.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
if (arr != null)
return decodeURIComponent(arr[2]);
return null;
}, /**
* 删除cookie
* @param name cookie name
*/
delCookie: function (name) {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = getCookie(name);
if (cval != null)
document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}, /**
* HTML编码
* @param str 待编码字符串
* @returns {string}
*/
html_encode: function (str) {
var s = "";
if (str.length == 0) return "";
s = str.replace(/&/g, ">");
s = s.replace(/</g, "<");
s = s.replace(/>/g, ">");
s = s.replace(/ /g, " ");
s = s.replace(/\'/g, "'");
s = s.replace(/\"/g, """);
s = s.replace(/\n/g, "<br>");
return s;
}, /**
* HTML解码
* @param str 待解码的字符串
* @returns {string}
*/
html_decode: function (str) {
var s = "";
if (str.length == 0) return "";
s = str.replace(/>/g, "&");
s = s.replace(/</g, "<");
s = s.replace(/>/g, ">");
s = s.replace(/ /g, " ");
s = s.replace(/'/g, "\'");
s = s.replace(/"/g, "\"");
s = s.replace(/<br>/g, "\n");
return s;
}, /**
* 光标停在文字的后面,文本框获得焦点时调用
*/
focusLast: function () {
var e = event.srcElement;
var r = e.createTextRange();
r.moveStart('character', e.value.length);
r.collapse(true);
r.select();
}, /**
* 生成一个新的GUID
* @return {string} 数据类型
* @method nuid
*/
nuid: function () {
return new Date().getTime().toString(36);
}
}

  

JS常用工具函数的更多相关文章

  1. JS常用工具函数(持续记录)

    1.设置获取cookie //方式1 //设置cookie function SetCookie(name, value)//两个参数,一个是cookie的名字,一个是值 { var Days = 3 ...

  2. 前端开发 —— js 常用工具函数(utilities)

    1. 时间 function getCurTime() { var date = new Date(); return date.toLocaleTimeString(); } date.toLoca ...

  3. Node.js 常用工具

    Node.js 常用工具 util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简的不足. util.inherits util.inherit ...

  4. Node.js 常用工具util包

    Node.js 常用工具 util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简的不足. util.isError(obj); util.is ...

  5. js常用工具类.

    一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...

  6. JS常用自定义函数总结

    JS常用自定义函数总结   1.原生JavaScript实现字符串长度截取 2.原生JavaScript获取域名主机 3.原生JavaScript清除空格 4.原生JavaScript替换全部 5.原 ...

  7. numpy 常用工具函数 —— np.bincount/np.average

    numpy 常用工具函数 —— np.bincount/np.average numpy 常用api(一) numpy 常用api(二) 一个函数提供 random_state 的关键字参数(keyw ...

  8. JS开发常用工具函数 总结

    js原生工具库 1.isStatic:检测数据是不是除了symbol外的原始数据 */ function isStatic(value) { return( typeof value === 'str ...

  9. JavaScript常用工具函数

    检测数据是不是除了symbol外的原始数据 function isStatic(value) { return ( typeof value === 'string' || typeof value ...

随机推荐

  1. JavaScript:substr vs substring vs slice

    参考文章: JavaScript取子串方法slice,substr,substring对比表

  2. Win10/UWP开发-Ink墨迹书写

    在UWP开发中,微软提供了一个新型的InkCanvas控件用来让用户能书写墨迹,在新版的Edga浏览器中微软自己也用到了该控件使用户很方便的可以在web上做笔记. InkCanvas控件使用很简单,从 ...

  3. Struts2学习笔记

    一.struts2的工作原理 上图为struts整体结构. 1.客户端初始化一个指向servlet的请求: 2.请求通过一系列过滤器(其中的ActionContextCleanUp为可选过滤器,对st ...

  4. CSS hack方式一览【转】

    做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我们会极不情愿的使用这个不太友好的方式来达到大家要求的页面表现.我个人是不太推荐使用hack的,要知道 ...

  5. iptables的扩展匹配

    iptables的匹配条件 一.通用匹配:-s.-d.-p.-i.-o 二.扩展匹配 1.隐含扩展:使用-p{tcp|udp|icmp}指定某特定协议后,自动能够对协议进行扩展 -p tcp --dp ...

  6. vm虚拟机安装雨林木风ghost镜像

    每次安装总是提示没办法加载镜像,或者镜像不存在,总之就是读取不到光驱里的镜像文件. 这是需要注意的两点:cd光驱模式设置为IDE,不能是scsi和sata两种模式,然后再进入winpe系统就行.

  7. Linear Algebra Lecture5 note

    Section 2.7     PA=LU and Section 3.1   Vector Spaces and Subspaces   Transpose(转置) example: 特殊情况,对称 ...

  8. Python requests 为pfsense 添加Routes

    # !/usr/bin/python 2 # -*- coding: utf-8 -*- __author__ = "Evilxr" import requests ips = o ...

  9. lua-nginx-module 学习

    下载安装LuaJIT cd /usr/local/src sudo wget http://luajit.org/download/LuaJIT-2.0.3.tar.gz tar -xzvf LuaJ ...

  10. SQL NOT EXISTS

    看了一篇文章,虽然知识点很简单,但是还是帮我理解了一些以前没想到的东西 一共三个表student,class,score create table student(sno varchar(50) no ...