Rumah >hujung hadapan web >tutorial js >discuz中用到的javascript函数解析[原创]第1/2页_javascript技巧
var lang = new Array();
var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
function $(id) {
return document.getElementById(id);
}
//定义数组的push属性
Array.prototype.push = function(value) {
this[this.length] = value;
return this.length;
}
//javascript全选函数
function checkall(form, prefix, checkall) {
var checkall = checkall ? checkall : 'chkall';
for(var i = 0; i < form.elements.length; i++) {
var e = form.elements[i];
if(e.name && e.name != checkall && (!prefix || (prefix && e.name.match(prefix)))) {
e.checked = form.elements[checkall].checked;
}
}
}
function doane(event) {
e = event ? event : window.event;
if(is_ie) {
e.returnValue = false;
e.cancelBubble = true;
} else if(e) {
e.stopPropagation();
e.preventDefault();
}
}
function fetchCheckbox(cbn) {
return $(cbn) && $(cbn).checked == true ? 1 : 0;
}
function getcookie(name) {
var cookie_start = document.cookie.indexOf(name);
var cookie_end = document.cookie.indexOf(";", cookie_start);
return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
}
function thumbImg(obj) {
var zw = obj.width;
var zh = obj.height;
if(is_ie && zw == 0 && zh == 0) {
var matches;
re = /width=(["']?)(\d+)(\1)/i;
matches = re.exec(obj.outerHTML);
zw = matches[2];
re = /height=(["']?)(\d+)(\1)/i;
matches = re.exec(obj.outerHTML);
zh = matches[2];
}
obj.resized = true;
obj.style.width = zw + 'px';
obj.style.height = 'auto';
if(obj.offsetHeight > zh) {
obj.style.height = zh + 'px';
obj.style.width = 'auto';
}
if(is_ie) {
var imgid = 'img_' + Math.random();
obj.id = imgid;
setTimeout('try {if ($(\''+imgid+'\').offsetHeight > '+zh+') {$(\''+imgid+'\').style.height = \''+zh+'px\';$(\''+imgid+'\').style.width = \'auto\';}} catch(e){}', 1000);
}
obj.onload = null;
}
function imgzoom(obj) {}
function in_array(needle, haystack) {
if(typeof needle == 'string' || typeof needle == 'number') {
for(var i in haystack) {
if(haystack[i] == needle) {
return true;
}
}
}
return false;
}
function setcopy(text, alertmsg){
if(is_ie) {
clipboardData.setData('Text', text);
alert(alertmsg);
} else if(prompt('Press Ctrl+C Copy to Clipboard', text)) {
alert(alertmsg);
}
}
function isUndefined(variable) {
return typeof variable == 'undefined' ? true : false;
}
function mb_strlen(str) {
var len = 0;
for(var i = 0; i < str.length; i++) {
len += str.charCodeAt(i)