/**
* 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. Maven Test

    Failures表示要测试的结果与预期值不一致:Errors表示测试代码或产品代码发生了未预期的错误:Skipped表示那些被标记为忽略的测试方法.在Junit中用户可以使用@Ignore注解标记忽略 ...

  2. C# Substring的用法

    方法1  Substring(Int32) 从此实例检索子字符串. 子字符串在指定的字符位置开始并一直到该字符串的末尾. 方法2 Substring(Int32, Int32) 从此实例检索子字符串. ...

  3. LayaAir引擎——(九)

    var h = new Array(); var j = new Array(); var xbCursor = 0; function xbinit() { xbinitName(); xbRect ...

  4. java.lang.ClassCastException: org.slf4j.impl.Log4jLoggerFactory cannot be cast to ch.qos.logback.classic.LoggerContext问题原因及解决方法

    一.错误信息 java.lang.ClassCastException: org.slf4j.impl.Log4jLoggerFactory cannot be cast to ch.qos.logb ...

  5. Windows下VTK6.0.0安装详解(CMake使用说明)

    操作系统:Windows7,用到工具:Visual studio.CMake. 1.准备工作 VTK下载: 下载最新VTK稳定版(6.0.0,截至2013年7月)http://www.vtk.org/ ...

  6. theano中的scan用法

    scan函数是theano中的循环函数,相当于for loop.在读别人的代码时第一次看到,有点迷糊,不知道输入.输出怎么定义,网上也很少有example,大多数都是相互转载同一篇.所以,还是要看官方 ...

  7. PHPMyAdmin弱口令猜解【Python脚本】

    PHPMyAdmin弱口令猜解 测试截图: 代码片段 #! /usr/bin/env python # _*_ coding:utf-8 _*_ import requests import time ...

  8. Call me evilxr

    我是Evilxr,博客新开第一天:希望自己能坚持下去!之前友友推荐的是oschina,使用了一段时间发现oschina经常抽风,再者自己平时也有关注一些博客觉得cnblogs也不错,开博的初衷是想记录 ...

  9. 转载list

    Linux系统下安装rz/sz命令及使用说明 http://blog.csdn.net/kobejayandy/article/details/13291655

  10. Get Jenkins job build queue length

    Jenkins API doesn’t provide the job build queue length. Hence, it seems we have to parse the html to ...