tools.js
11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/**
* @fileOverview 前后端的公用工具类和方法封装
* @author:Hbomb(zhouqq@yoho.cn)
* @date:2012-07-09
*/
define("lib/util/tools",[],function(require, exports,module)
{
/**
* HTML编码
*/
exports.escapeHTML = function (ret)
{
if (!ret)
{
return;
}
ret = exports.unescapeHTML(ret);
var div = document.createElement('div');
var text = document.createTextNode(ret);
div.appendChild(text);
return div.innerHTML;
};
/**
* HTML反编码
*/
exports.unescapeHTML = function (str)
{
str=str+"";
var ret = str.replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&');
ret = ret.replace(/"/g, '"');
return ret;
};
/**
* js对象克隆
*/
exports.IYOHO_clone = function (myObj)
{
if (typeof(myObj) != 'object')
return myObj;
if (myObj == null)
return myObj;
var myNewObj = {};
if(myObj.length)
{
myNewObj = [];
}
for (var i in myObj)
myNewObj[i] = IYOHO_clone(myObj[i]);
return myNewObj;
};
/**
* js object2Array
*/
exports.IYOHO_Object2Array = function (obj)
{
var arr = [];
for(var i in obj)
{
arr.push(obj[i]);
}
return arr;
};
/**
* 时间格式化yyyy/mm/dd
*/
exports.timeFormat = function (dt, format)
{
var dd = dt.getDate();
var mm = dt.getMonth() + 1; // January is 0!
var yyyy = dt.getFullYear();
if (dd < 10)
{
dd = '0' + dd;
}
if (mm < 10)
{
mm = '0' + mm;
}
return yyyy + format + mm + format + dd;
};
/**
* 获取url参数
*/
exports.request = function (strName)
{
var strHref = window.document.location.href;
return _request(strHref, strName);
};
/**
* 获取url参数
*/
function _request(href, strName)
{
var strHref = href;
var intPos = strHref.indexOf("?");
var strRight = strHref.substr(intPos + 1);
var arrTmp = strRight.split("&");
for (var i = 0; i < arrTmp.length; i++)
{
var arrTemp = arrTmp[i].split("=");
if (arrTemp[0].toUpperCase() == strName.toUpperCase())
return arrTemp[1];
}
return "";
}
/**
* cookie set and get
*/
exports.cookie = function (name, value, options)
{
if (typeof value != 'undefined')
{
options = options || {};
if (value === null)
{
value = '';
//options = $.extend({}, options);
options.expires = -1;
}
var expires = '';
if (options.expires &&
(typeof options.expires == 'number' || options.expires.toUTCString))
{
var date;
if (typeof options.expires == 'number')
{
date = new Date();
date.setTime(date.getTime() +
(options.expires * 24 * 60 * 60 * 1000));
}
else
{
date = options.expires;
}
expires = '; expires=' + date.toUTCString();
}
var path = options.path ? '; path=' + (options.path) : '';
var domain = options.domain ? '; domain=' + (options.domain) : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
}
else
{
var cookieValue = null;
if (document.cookie && document.cookie != '')
{
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++)
{
var cookie = exports.trim(cookies[i]);
if (cookie.substring(0, name.length + 1) == (name + '='))
{
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};
exports.trim = function( text )
{
if(String.prototype.trim)
{
return text == null?
"":
String.prototype.trim.call( text );
}
else
{
var trimLeft = /^\s+/;
var trimRight = /\s+$/;
return text == null?
"":
text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
}
};
/**
* 检查email邮箱
*/
exports.IYOHO_isEmail = function (str)
{
var reg = /^\s*([A-Za-z0-9_-]+(\.\w+)*@(\w+\.)+\w{2,3})\s*$/;
return reg.test(str);
};
/**
* 检查qq号
*/
exports.IYOHO_isQQ = function (str)
{
var reg = /^[1-9]\d{4,10}$/;
return reg.test(str);
};
/**
* 检查电话
*/
exports.IYOHO_isTel = function (str)
{
var reg = /^\s*[.0-9]{8,11}\s*$/;
return reg.test(str);
};
/**
* 检查邮编
*/
exports.IYOHO_isPostcode = function(str)
{
var reg = /^[0-9]\d{5}(?!\d)/;
return reg.test(str);
};
/**
* 检测姓名非法字符
*/
exports.IYOHO_isUserName = function (str)
{
var reg = /^[^@\/\'\\\"#$%&\^\*]+$/;
return reg.test(str);
};
/**
* 检测是否合法url
*/
exports.IYOHO_isURL = function (str)
{
var reg = /^((http(s)?|ftp|telnet|news|rtsp|mms):\/\/)?(((\w(\-*\w)*\.)+[a-zA-Z]{2,4})|(((1\d\d|2([0-4]\d|5[0-5])|[1-9]\d|\d).){3}(1\d\d|2([0-4]\d|5[0-5])|[1-9]\d|\d).?))(:\d{0,5})?(\/+.*)*$/;
return reg.test(str);
};
/**
* 根据日期获取星座
*/
/**
* 摩羯座(12/22 - 1/19)、水瓶座(1/20 - 2/18)、双鱼座(2/19 - 3/20)、牡羊座(3/21 - 4/20)、金牛座(4/21 -
* 5/20)、 双子座(5/21 - 6/21)、巨蟹座(6/22 - 7/22)、狮子座(7/23 - 8/22)、处女座(8/23 -
* 9/22)、天秤座(9/23 - 10/22)、 天蝎座(10/23 - 11/21)、射手座(11/22 - 12/21)
*/
exports.getZodiac = function (month, day)
{
var d = new Date(1999, month - 1, day, 0, 0, 0);
var arr = [];
arr.push(["摩羯座", new Date(1999, 0, 1, 0, 0, 0)]);
arr.push(["水瓶座", new Date(1999, 0, 20, 0, 0, 0)]);
arr.push(["双鱼座", new Date(1999, 1, 19, 0, 0, 0)]);
arr.push(["牧羊座", new Date(1999, 2, 21, 0, 0, 0)]);
arr.push(["金牛座", new Date(1999, 3, 21, 0, 0, 0)]);
arr.push(["双子座", new Date(1999, 4, 21, 0, 0, 0)]);
arr.push(["巨蟹座", new Date(1999, 5, 22, 0, 0, 0)]);
arr.push(["狮子座", new Date(1999, 6, 23, 0, 0, 0)]);
arr.push(["处女座", new Date(1999, 7, 23, 0, 0, 0)]);
arr.push(["天秤座", new Date(1999, 8, 23, 0, 0, 0)]);
arr.push(["天蝎座", new Date(1999, 9, 23, 0, 0, 0)]);
arr.push(["射手座", new Date(1999, 10, 22, 0, 0, 0)]);
arr.push(["摩羯座", new Date(1999, 11, 22, 0, 0, 0)]);
for (var i = arr.length - 1; i >= 0; i--)
{
if (d >= arr[i][1])
return arr[i][0];
}
};
/**
* 根据年份获取生肖
*/
exports.getShengxiao= function (yyyy)
{
var arr = ['猴', '鸡', '狗', '猪', '鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊'];
return /^\d{4}$/.test(yyyy) ? arr[yyyy % 12] : null;
};
/**
* 数组去重复
*/
exports.unique = function (data)
{
data = data || [];
var a = {};
for (var i = 0, len = data.length; i < len; i++)
{
var v = data[i];
if ('undefined' == typeof(a[v]))
{
a[v] = 1;
}
}
data.length = 0;
for (var i in a)
{
data[data.length] = i;
}
return data;
};
/**
* 生成一个不重复的别名
*/
exports.genDefID = function(str)
{
var time = new Date().getTime() + "";
var id = str + "_" + (time).substring(time.length - 6) + "_" +
(Math.round(Math.random() * 1000));
return id;
};
/**
* 获取纯字符长度
*/
exports.fucCheckLength = function (strTemp)
{
var i, sum;
sum = 0;
for (i = 0; i < strTemp.length; i++)
{
if ((strTemp.charCodeAt(i) >= 0) && (strTemp.charCodeAt(i) <= 255))
sum = sum + 1;
else
sum = sum + 2;
}
return sum;
};
/**
* 截字封装(字符串,长度)
*/
exports.limitWords = function (str, len)
{
var ret = "";
var str_ = exports.unescapeHTML(str);
if (exports.fucCheckLength(str_) > len)
{
str = exports.unescapeHTML(str);
ret = subStringByByte(str, 0, len - 1);
ret = exports.escapeHTML(ret);
ret += "...";
}
else
{
ret = str;
}
return ret;
};
/**
* 截字
* @param {} str 需要截字的字符串
* @param {} idx1 开始位置
* @param {} idx2 结束位置
* @return {}
*/
function subStringByByte (str, idx1, idx2)
{
var n = 0, start = str.length, end = str.length;
for (var i = 0; i < str.length; i++)
{
if (n <= idx1)
{
start = i;
}
if (str.charCodeAt(i) < 128)
{
n++;
}
else
{
n += 2;
}
if (n > idx2)
{
end = i;
break;
}
}
return str.substring(start, end);
};
/**
* 检测Capslock按键
*/
exports.checkCapsLock = function (e)
{
valueCapsLock = e.which;
if (valueCapsLock >= 65 && valueCapsLock <= 90 && !e.shiftKey)
{
return true;
}
else
{
return false;
}
};
/**
* 获得DOM窗口相对于window的位置
*/
exports.getPos = function (obj)
{
var curLeft = 0, curTop = 0;
if (obj.offsetParent)
{
while(obj.offsetParent)
{
curLeft += obj.offsetLeft;
curTop += obj.offsetTop;
obj = obj.offsetParent;
}
};
return [curTop, curLeft];
};
/**
* 获得相关的属性的方法
*/
exports.getCssVal = function (o, key)
{
return o.currentStyle? o.currentStyle[key] : document.defaultView.getComputedStyle(o,false)[key];
};
/**
* 判断是否数组
*/
exports.isArray = function(params)
{
return Object.prototype.toString.apply(params) === '[object Array]';
};
/**
* 日期的格式化
*/
Date.prototype.format = function(format) //author: meizz
{
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
};
if(/(y+)/.test(format))
{
format=format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o)
{
if(new RegExp("("+ k +")").test(format))
{
format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
}
return format;
};
//获取flash对象
exports.getFlash = function (movieName)
{
if (navigator.appName.indexOf("Microsoft") != -1&&navigator.appVersion.indexOf("MSIE 9.0")==-1)
{
return window[movieName];
}
else
{
return document[movieName];
}
};
//判断是否有history
exports.hasHistory = function()
{
if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0))
{
if(history.length>0)
{
return true;
}
else
{
return false;
}
}
else
{
if(history.length>1)
{
return true;
}
else
{
return false;
}
}
};
});