作者: zhongxia
事件: 2016-08-04 16:59:09
-
禁止鍵盤回退鍵,使瀏覽器頁面回退
/**
* 禁用瀏覽器回退事件
* @param e
* @returns {boolean}
*/
function forbidBackSpace(e) {
console.info("禁用鍵盤回退鍵,導(dǎo)致頁面回退...")
var ev = e || window.event; //獲取event對象
var obj = ev.target || ev.srcElement; //獲取事件源
var t = obj.type || obj.getAttribute('type');
var vReadOnly = obj.readOnly;
var vDisabled = obj.disabled;
//處理undefined值情況
vReadOnly = (vReadOnly == undefined) ? false : vReadOnly;
vDisabled = (vDisabled == undefined) ? true : vDisabled;
//當(dāng)敲Backspace鍵時潘明,事件源類型為密碼或單行章姓、多行文本的,
//并且readOnly屬性為true或disabled屬性為true的麦萤,則退格鍵失效
var flag1 = ev.keyCode == 8 && (t == "password" || t == "text" || t == "textarea") && (vReadOnly == true || vDisabled == true);
//當(dāng)敲Backspace鍵時,事件源類型非密碼或單行若未、多行文本的沸伏,則退格鍵失效
var flag2 = ev.keyCode == 8 && t != "password" && t != "text" && t != "textarea";
//判斷
if (flag2 || flag1) return false;
}
-
生成唯一標(biāo)識
/**
* 生成UUID
* @param len 指定生成項(xiàng)的長度
* @param radix 范圍 比如設(shè)置 2 , 就只會生成 0101010之類的
* @returns {string}
*/
function generatorUUID(len, radix) {
var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
var uuid = [], i;
radix = radix || chars.length;
if (len) {
for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
} else {
var r;
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
uuid[14] = '4';
for (i = 0; i < 36; i++) {
if (!uuid[i]) {
r = 0 | Math.random() * 16;
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
}
}
}
return uuid.join('');
}