一些常用的JS功能函数代码

代码如下:

//获取对象
function getObject(objectId,top)
{
doc = top?window.top.document:document;
if(typeof(objectId)!="object" && typeof(objectId)!="function")
{
if(doc.getElementById && doc.getElementById(objectId))
{
// W3C DOM
return doc.getElementById(objectId);
}
else if(doc.getElementsByName(objectId))
{
return doc.getElementsByName(objectId)[0];
}
else if (doc.all && doc.all(objectId))
{
// MSIE 4 DOM
return doc.all(objectId);
}
else if (doc.layers && doc.layers[objectId])
{
// NN 4 DOM.. note: this won't find nested layers
return doc.layers[objectId];
}
else
{
return false;
}
}else
return objectId;
}
//获取相对路径
function getRelativePath()
{
var url = location.href;//当前url
var urlcs = String(location.search);
url = url.replace(urlcs,"");
var path = url.length - url.replace(/\//g,"").length - 3; //层次为url包含/的长度-没有包含/的长度再减去项目头/的个数

var str = "";
for(var i = 0; i < path; i++)
{
str += "../";//组合成一个相对路径的字符串返回
}
return str;
}
//加载其他JS文件或CSS文件
function loadjscssfile(filename,filetype,chkonce)
{
filetype = !filetype?"js":filetype;
var had = false;
if(filetype=="js")
{
if(chkonce)
{
var allScripts = document.getElementsByTagName("script");
for(var i=0;i<allScripts.length;i++)
{
try{
if(allScripts[i].src.indexOf(filename)>-1)
{
had = true;
break;
}
}catch(e){}
}
}
if(!had)
{
document.write("<script src=""+filename+"" src=""+filename+"" type='text/javascript'></script>");
}
}else
{
if(chkonce)
{
var allCss = document.getElementsByTagName("link");
if(allCss.length)
{
for(var i=0;i<allCss.length;i++)
{
try{
if(allCss[i].href.indexOf(filename)>-1)
{
had = true;
break;
}
}catch(e){}
}
}
}
if(!had)
{
document.write("<link type='text/css' rel='stylesheet' href=""+filename+"" href=""+filename+"" />");
}
}
}
//定义根目录路径
var ROOT_PATH = getRelativePath();
var JS_PATH = ROOT_PATH+'js/';
var AJAX_PATH = ROOT_PATH+'ajax/';
var CSS_PATH = ROOT_PATH+'css/';
var IMAGES_PATH = ROOT_PATH+'images/';
var EDITOR_PATH = ROOT_PATH+'uploadeditor/';
var PUB_PATH = ROOT_PATH+'uploadfile/';

//加载公共变量的JS
loadjscssfile(JS_PATH+"globalPara.js");

//设置下拉表中某一项被选中
function setSelOption(objId,vlu)
{
objId = getObject(objId);
for(var i=0;i<objId.options.length;i++)
{
if(objId.options[i].value==vlu)
{
objId.options[i].setAttribute("selected","selected");
break;
}
}
}
//根据下拉表中的option文本设置某一项被选中
function setTxtOption(objId,txt)
{
objId = getObject(objId);
for(var i=0;i<objId.options.length;i++)
{
if(objId.options[i].innerHTML==txt)
{
objId.options[i].setAttribute("selected","selected");
break;
}
}
}

//设置单选按钮组中某一项被选中
function setSelRadio(objName,vlu)
{
objName = document.getElementsByName(objName);
for(var i=0;i<objName.length;i++)
{
if(objName[i].value==vlu)
{
objName[i].setAttribute("checked","checked");
break;
}
}
}

//根据ID设置复选框中某些项被选中
//vlu 的格式为 : 1,2,3
function setSelCheckbox(prefix,vlu)
{
var _arr = vlu.split(",");
if(_arr!="")
{
for(var i=0; i<_arr.length; i++)
{
getObject(prefix+_arr[i]).checked = true;
}
}
}

//快捷输入,fromObj:来源对象,toObjId:目标对象的ID,txt为true时取toObjId的innerHTML值
function fastInput(fromObj,toObjId,txt)
{
if(fromObj.value=='' || fromObj.value==0) return false;
txt = !txt ? false : txt;
var toObj = getObject(toObjId);
if(txt)
{
if(typeof (toObj.value) == 'undefined')
toObj.innerHTML = toTxt(fromObj.options[fromObj.selectedIndex].innerHTML);
else
toObj.value = toTxt(fromObj.options[fromObj.selectedIndex].innerHTML);
}else
{
if(typeof (toObj.value) == 'undefined')
toObj.innerHTML = fromObj.value;
else
toObj.value = fromObj.value;
}
}

/*
IE6,IE7上传图片前预览图片
IE6下还可以同时检测图片的大小
size 单位为KB
<style type="text/css"><!--

.newPreview
{
width:400px; height:300px;
FILTER: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)
}
.nodis{display:none;}
--></style><style type="text/css" bogus="1">
.newPreview
{
width:400px; height:300px;
FILTER: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)
}
.nodis{display:none;}</style>
<input type="file" name="litpic" onchange="PreviewImg(this,'newPreview','ndsPreview',400)" />
<div id="newPreview" class="newPreview"></div>
<img id="ndsPreview" class="nodis" />
*/
function PreviewImg(imgFile,newPreview,ndsPreview,size)
{
newPreview = getObject(newPreview);
if(!imgFile || !imgFile.value || !newPreview){return};
var patn = /\.jpg$|\.jpeg$|\.gif$|\.png$|\.bmp$/i;
if(patn.test(imgFile.value))
{
try{newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile.value;}catch(e){}
if(navigator.appVersion.indexOf("MSIE 6.0",0)>-1)
{
size=!size?0:size;
ndsPreview = getObject(ndsPreview);
if(ndsPreview)
{
try{ndsPreview.attachEvent("onreadystatechange", function(){checkImgSize(ndsPreview,size)})}catch(e){}
ndsPreview.src = imgFile.value;
}
}
}
else
{
alert("您选择的不是图像文件,请重新选择.");
}
}
function PreviewImgNow(imgDiv,imgFile)
{
try{getObject(imgDiv).filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile}catch(e){}
}
//上传之前检测图片的大小
//条件是在file改变时要触发函数将缩略图显示在img上
//size 单位为KB
function checkImgSize(img,size)
{
img = getObject(img);
if(img.readyState == "complete")
{
var limit = size * 1024;
if(img.fileSize > limit)
{
alert("出错!您上传的图片大小为"+(parseInt(img.fileSize/1024))+"KB,超过了"+size+"KB的限制,请重新上传");
return false
}else
return true;
}
return true;
}
//检测客户端环境
function ClientMentInfo()
{
var me = this;
var appVer = navigator.userAgent;

this.GetBrowserName = function (){
if(appVer.indexOf("MSIE")>0) return "IE";
else if(appVer.indexOf("Firefox")>0) return "Firefox";
else if(appVer.indexOf("Chrome")>0) return "Chrome";
else if(appVer.indexOf("Safari")>0) return "Safari";
else if(appVer.indexOf("Camino")>0) return "Camino";
else if(appVer.indexOf("Konqueror")>0) return "Konqueror";
else return "其它";
}

this.GetOSInfo = function (){
var _pf = navigator.platform;
if(_pf == "Win32" || _pf == "Windows")
{

if(appVer.indexOf("Windows NT 6.0") > -1 || appVer.indexOf("Windows Vista") > -1)
{
return 'Windows Vista';
}else if(appVer.indexOf("Windows NT 6.1") > -1 || appVer.indexOf("Windows 7") > -1)
{
return 'Windows 7';
}else
{
try{
var _winName = Array('2000','XP','2003');
var _ntNum = appVer.match(/Windows NT 5.\d/i).toString();
return 'Windows ' + _winName[_ntNum.replace(/Windows NT 5.(\d)/i,"$1")];
}catch(e){return 'Windows';}
}
}else if(_pf == "Mac68K" || _pf == "MacPPC" || _pf == "Macintosh")
{
return "Mac";
}else if(_pf == "X11")
{
return "Unix";
}else if(String(_pf).indexOf("Linux") > -1)
{
return "Linux";
}else
{
return "Unknow";
}
}

this.OS = me.GetOSInfo(); //操作系统类型
this.IeVer = null;
this.Bs_lang = (navigator.appName == 'Netscape'?navigator.language:navigator.browserLanguage);//浏览器语言版本
this.Bs_Name = me.GetBrowserName(); //浏览器名称

//浏览器版本
if(this.Bs_Name=='IE')
{
var _msie = appVer.match(/MSIE \d./i).toString();
this.Bs_Version = this.IeVer = _msie.replace(/MSIE (\d)./i,"$1");
}else
{
this.Bs_Version = appVer;
}
this.Ie6 = this.IeVer==6 ? true: false;
this.Ie7 = this.IeVer==7 ? true: false;
this.Ie8 = this.IeVer==8 ? true: false;
}
//客户端信息
var CMInfo = new ClientMentInfo();
//IE6 下缓存背景图片
if(CMInfo.Ie6)
{
document.execCommand("BackgroundImageCache", false, true);
}
//设置复选框全选或全不选
function setAllCheckbox(formName,objName,num)
{
if(formName)
_arr = getObject(formName).elements[objName];
else
_arr = typeof(objName)=="object"?objName:document.all(objName);
if(_arr)
{
if(num)
{
if (!_arr.length ) // 只有一个复选框,则length = undefined
_arr.checked = true;
else
{
for(var i=0; i<_arr.length; i++)
{
_arr[i].checked = true;
}
}
}else
{
if (!_arr.length ) // 只有一个复选框,则length = undefined
_arr.checked = false;
else
{
for(var i=0; i<_arr.length; i++)
{
_arr[i].checked = false;
}
}
}
}
}

//使当前页面跳到指定的页数页面
function goPage(pageNum,pageStr)
{
window.location.href = "?np="+pageNum+pageStr;
}
//分别去字符串前后,左边,右边空格
String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,"")}
String.prototype.ltrim = function(){ return this.replace(/^\s+/g,"")}
String.prototype.rtrim = function(){ return this.replace(/\s+$/g,"")}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/linyuanxing/archive/2008/12/01/3421058.aspx

注意globalPara.js本作者常用到的一些每个站自己的常用变量和常量的文件,如果你不需要这样的文件,可把
//加载公共变量的JS
loadjscssfile(JS_PATH+"globalPara.js");
这两句删掉,不然会报错。
(2009-06-04更新)
将 ClientMentInfo类改成了兼容IE6,IE7,IE8,Vista,Windows 7和Firefox

(0)

相关推荐

  • 一些常用的JS功能函数代码

    复制代码 代码如下: //获取对象 function getObject(objectId,top) { doc = top?window.top.document:document; if(typeof(objectId)!="object" && typeof(objectId)!="function") { if(doc.getElementById && doc.getElementById(objectId)) { // W

  • 一些常用的JS功能函数(2009-06-04更新)

    复制代码 代码如下: //获取对象 function getObject(objectId,top) {     doc = top?window.top.document:document;     if(typeof(objectId)!="object" && typeof(objectId)!="function")     {         if(doc.getElementById && doc.getElementBy

  • php实现的三个常用加密解密功能函数示例

    本文实例讲述了php实现的三个常用加密解密功能函数.分享给大家供大家参考,具体如下: 算法一: //加密函数 function lock_url($txt,$key='www.jb51.net') { $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+"; $nh = rand(0,64); $ch = $chars[$nh]; $mdKey = md5($key.$ch); $mdKe

  • 常用原生js自定义函数总结

    js获取日期函数 //获取当前时间日期 function CurentTime() { var now = new Date(); var year = now.getFullYear(); //年 var month = now.getMonth() + 1; //月 var day = now.getDate(); //日 var hh = now.getHours(); //时 var mm = now.getMinutes(); //分 var clock = year + "-&quo

  • Js四则运算函数代码

    复制代码 代码如下: //除法函数,用来得到精确的除法结果 //说明:javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显.这个函数返回较为精确的除法结果. //调用:accDiv(arg1,arg2) //返回值:arg1除以arg2的精确结果 function accDiv(arg1,arg2){ var t1=0,t2=0,r1,r2; try{t1=arg1.toString().split(".")[1].length}catch(e){} try{t2=

  • 心扬JS分页函数代码

    复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=&qu

  • asp.net 文件下载功能函数代码整理

    复制代码 代码如下: public void FileDownLoadDel(string fullFilename) { System.IO.Stream iStream = null; // Buffer to read 10K bytes in chunk: byte[] buffer = new Byte[10000]; // Length of the file: int length; // Total bytes to read: long dataToRead; // Ident

  • js打印纸函数代码(递归)

    复制代码 代码如下: //打印纸尺寸,单位MM //http://en.wikipedia.org/wiki/ISO_216 var page = function() { var A0 = { name: "A0", width: 841, height: 1189 }; //单位MM var B0 = { name: "B0", width: 1000, height: 1414 }; //单位MM var C0 = { name: "C0"

  • 常用的JS验证和函数汇总

    下面是我常用一些JS验证和函数,有一些验证我直接写到了对象的属性里面了,可以直接通过对象.方法来调用 复制代码 代码如下: //浮点数除法运算 function fdiv(a, b, n) {     if (n == undefined) { n = 2; }     var t1 = 0, t2 = 0, r1, r2;     try { t1 = a.toString().split(".")[1].length } catch (e) { }     try { t2 = b

  • JS功能代码集锦

    1.模仿fade in(),fade out(). 原理:setInterval ( "opacity++透明度"函数,时间间隔) var alpha = 0; function play(){ timer = setInterval(function(){ alpha += 2; alpha > 100 && (alpha = 100); aImg[index].style.opacity = alpha / 100; aImg[index].style.fil

随机推荐