Javascript函数-备忘录(转)

/检测文件扩展名
function EuwCheckExt(path,extList){
var ext = getFileExt(path);
var cExt = extList.indexOf("," + ext + ",");
if (ext == "") return -1;
if (cExt){
return 1;
}else{
return 0;
}

}

//取得文件扩展名
function getFileExt(path){
var tmp = path;
tmp = tmp.substring(tmp.lastIndexOf(".")+1);
return tmp.toUpperCase();

}

//操作样式
function init(a){
var styleText1 = "";
styleText1 +="font-family: Arial, Verdana;";
styleText1 +="font-size: 18pt;";
styleText1 +="font-weight: bold;";
styleText1 +="font-style: italic;";
styleText1 +="color: #33CC00;";
//styleText1 +="background-color: #CCCC00;";
styleText1 +="filter: DropShadow(Color=#000000, OffX=1, OffY=1,
Positive=0);";
tdTmp.style.cssText=styleText1;

}

//粘贴前过滤剪贴板数据
function EuwColorOnBeforePaste(objColor){
clipboardData.setData('text',clipboardData.getData('text').replace(/[^a-fA-F0-9]/g,'').toUpperCase());

}

//颜色值输入
function EuwColorOnKeyUp(objColor){

objColor.value=objColor.value.replace(/[^#a-fA-F0-9]/g,'').toUpperCase();

}

//setTimeout()应用
function loopshow(name){
xxx.location="a.jpg";
setTimeout("loopshow(" + name + ");",100);

}

//数字输入
function EuwCheckNumOnKeyUp(obj){
obj.value=obj.value.replace(/[^0-9.]/g,'').toUpperCase();

}

//按比例缩放图片
var loadNum=0;
var loadTimes = 3;
checkShowSize = false;
function showsize()
{
//document.upload.FILE1.value = trim(document.upload.FILE1.value);
document.all.view.src = document.upload.FILE1.value;
//alert(document.all.view.height + "x" + document.all.view.width + " -> " +
document.all.view.fileSize);

var diskuse = 5190970
var quota = 5242880
var maxfilesize = 512000;
var showimg = new Image();
Image.create(22,22).
showimg.src = document.upload.FILE1.value;
alert(showimg.fileSize);
if(showimg.fileSize>0)
{
if(showimg.fileSize > maxfilesize)
{
alertmsg = "您选中的图片大于" + maxfilesize+ ",目前字节数=" +
showimg.fileSize;
return false;
}

if(showimg.fileSize > quota - diskuse) {
alertmsg = "您选中的图片文件过大,或者相册已满";
return false;
}

if(showimg.width > 100)
{
alert(showimg.width);
var ori_w= showimg.width;
var ori_h = showimg.height;
showimg.width = 100;
showimg.height = ori_h * showimg.width / ori_w;
}

if(showimg.width < 5 || showimg.height <5)
{
showimg.width = showimg.height = 150;
}

//document.all.view.src = showimg.src;
document.all.view.style.width = showimg.width;
document.all.view.style.height = showimg.height;
checkShowSize = true;
return true;
} else if(loadNum
loadNum ++ ;
setTimeout('showsize()',500);
} else if(loadNum>=loadTimes){
loadNum = 0;
alertmsg = "图片字节数=0,请换张图片上传";
return false;
}

}

//字符串长度属性
String.prototype.len=function()
{
return this.replace(/[^\x00-\xff]/g,"**").length;

}

//日期自动补0
function appendZero(n){return(("00"+ n).substr(("00"+ n).length-2));}

//字符串去空格属性
function String.prototype.trim(){return this.replace(/(^\s*)|(\s*$)/g,"");}

//打开一个全屏或者居中的窗口
function openwindow( url, winName,width,height,otherproperty)
{
//width,height
//otherproperty
xposition=0; yposition=0;
if ((parseInt(navigator.appVersion) >= 4 ))
{
if(width == 1)
{
width=screen.width - 10;
height=screen.height - 55;
xposition = 0;
yposition = 0;
}
else
{
if (width < 1)
{
width=screen.width*width;
height=screen.height*height;
}
xposition = (screen.width - width) / 2;
yposition = (screen.height - height) / 2 - 15;
}

if (yposition < 0 )
{
yposition = 0;
}
}
theproperty= "width=" + width + ", " + "height=" + height + ", "
+ "screenX=" + xposition + ", " //Netscape
+ "screenY=" + yposition + ", " //Netscape
+ "left=" + xposition + ", " //IE
+ "top=" + yposition + ", "; //IE

theproperty = theproperty + "location=0, "
+ "menubar=0, "
+ "resizable=0, "
+ "scrollbars=1, "
+ "status=0, "
+ "toolbar=0, "
+ "hotkeys=0, ";
theproperty = theproperty + ', ' + otherproperty;
winobj=window.open( url,winName,theproperty );
if (url == "about:blank")
{
winobj.document.writeln("Loading.....");
}
winobj.focus();
return winobj;

}

//给定图片的最大高宽,取得缩放的比例
function EuwResizeRatio(maxWidth,maxHeight, objImgTmp)
{
var hRatio;
var wRatio;
var Ratio = 1;
var maxHeight;
var maxWidth;
var width = objImgTmp.width;
var height = objImgTmp.height;
wRatio = EuwStr2Float(maxWidth / width,2);
hRatio = EuwStr2Float(maxHeight / height,2);
if (wRatio<1 || hRatio<1){
if (wRatio<=hRatio)
Ratio = wRatio
else
Ratio = hRatio
}
return Ratio;
}
//改进版本,结合setTimeout传递参数做法(脚本块中有此方法)
function EuwResizeRatio(maxWidth,maxHeight,objImgTmp,srcImg)
{
var hRatio;
var wRatio;
var Ratio = 1;
var isKernel = true;
if (objImgTmp==null) {isKernel = false};
if (isKernel && objImgTmp.width==0) {isKernel = false};
var width = 0;
var height = 0;
if (isKernel){
width = objImgTmp.width;
height = objImgTmp.height;
wRatio = EuwStr2Float(maxWidth / width,2);
hRatio = EuwStr2Float(maxHeight / height,2);
//alert(wRatio + "--" + hRatio);
if (maxWidth==0){//
if (hRatio<1) Ratio = hRatio;
}else if (maxHeight==0){
if (wRatio<1) Ratio = wRatio;
}else if (wRatio<1 || hRatio<1){
Ratio = (wRatio<=hRatio?wRatio:hRatio);
}
width = width * Ratio;
height = height * Ratio;
if (Ratio<1){
objImgTmp.width = width;
objImgTmp.height = height;
}
if (srcImg!=null){
srcImg.width = width;
srcImg.height = height;
}
}else{
var
timer=window.setTimeout(EuwResizeRatio,100,maxWidth,maxHeight,objImgTmp,srcImg);
}

}

function String.prototype.trim(){return this.replace(/(^\s*)|(\s*$)/g,"");}
function gf_AppendZero(n){return(("00"+ n).substr(("00"+ n).length-2));}//日期自动补零程序//
//取得当前系统日期时间
function gf_now()
{
var fs_now = new Date();
var fs_return="";
fs_return = fs_now.getFullYear() + "-" + gf_AppendZero(fs_now.getMonth() +
1) + "-" + gf_AppendZero(fs_now.getDate());
fs_return +=" " + gf_AppendZero(fs_now.getHours()) + ":" +
gf_AppendZero(fs_now.getMinutes()) + ":" +
gf_AppendZero(fs_now.getSeconds());
return fs_return;

}

//取得当前系统日期
function gf_today()
{
var fs_now = new Date();
var fs_return="";
fs_return = fs_now.getFullYear() + "-" + gf_AppendZero(fs_now.getMonth() +
1) + "-" + gf_AppendZero(fs_now.getDate());
return fs_return;

}

//验证日期是否合法(true/false)
function gf_chkdate(as_datestr)
{
var
fs_year="";fs_mon="";fs_day="";fs_time="";fs_hour="";fs_min="";fs_sec="";fs_obj="";fs_syn="";
var fi_loop,fi_len

fi_len = as_datestr.length
fs_obj = "year"
if (fi_len<=0) return true;
for (fi_loop=0;fi_loop<fi_len;fi_loop++ )
{
if (as_datestr.charAt(fi_loop)==" " && fs_obj == "day" ) fs_obj = "hour";
if (as_datestr.charAt(fi_loop)== '-'|| as_datestr.charAt(fi_loop)==
'/')
{
if (fs_syn == "")
{
fs_syn = as_datestr.charAt(fi_loop)
}
else
{
if (fs_syn != as_datestr.charAt(fi_loop)) return false //没有用统一的"-"或"/"符号错误
}

if (fs_obj == "year" )
{
fs_obj = "month"
}
else if (fs_obj == "month" )
{
fs_obj = "day"
}
}
else if (as_datestr.charAt(fi_loop)== ':')
{
if (fs_obj == "hour" )
{
fs_obj = "minute"
}
else if (fs_obj == "minute" )
{
fs_obj = "second"
}
}
else //取出相应的值
{
if (!(as_datestr.charAt(fi_loop) >= 0 && as_datestr.charAt(fi_loop) <=
9)) return false
if (fs_obj == "year" )
{
fs_year += as_datestr.charAt(fi_loop)
}
else if (fs_obj == "month" )
{
fs_mon += as_datestr.charAt(fi_loop)
}
else if (fs_obj == "day" )
{
fs_day += as_datestr.charAt(fi_loop)
}
else if (fs_obj == "hour" )
{
if (as_datestr.charAt(fi_loop)!= " ") fs_hour +=
as_datestr.charAt(fi_loop)
}
else if (fs_obj == "minute" )
{
fs_min += as_datestr.charAt(fi_loop)
}
else if (fs_obj == "second" )
{
fs_sec += as_datestr.charAt(fi_loop)
}
}
}
//验证时间的合法性
if (fs_hour != "")
{
if ( fs_min == "" )
{
return false
}
else
{
if (fs_sec == "") fs_sec = "0"
}
if ( fs_hour > 24 || fs_hour < 0 || fs_min >= 60 || fs_min < 0 || fs_sec
>= 60 || fs_sec < 0 )
{
return false //时间错误
}
if (fs_hour.length>2 || fs_min.length>2) return false
}
//验证日期的合法性
if ( fs_year == "" || fs_mon=="" || fs_day=="" ) return false;
if (fs_year.length == 2 )
{
fs_year = '20'+ fs_year;
}
else if (fs_year.length != 4 )
{
return false;
}
if ( fs_year.length > 4 || fs_mon.length > 2 || fs_day.length > 2 ) return
false;
if (!(fs_year>1901 && fs_year < 2999 && 1<=fs_mon && 12>=fs_mon &&
31>=fs_day && 1<=fs_day) ) return false;
if (( fs_mon==4 || fs_mon==6 || fs_mon==9 || fs_mon==11 ) && (fs_day>30))
return false;
if ((fs_year % 4 == 0) && (fs_mon==2) && (fs_day > 29)) return false;
if (!(fs_year % 4 == 0) && (fs_mon==2) && (fs_day > 28)) return false;

return true;

}

//验证只能录入整数onkeypress
function gf_checkint()
{
if ((window.event.keyCode<48 || window.event.keyCode>57) &&
window.event.keyCode!=45 ) window.event.returnValue=false;

}

//验证只能录入实数onkeypress
function gf_checkobjfloat(objectname)
{
if ((window.event.keyCode<45 || window.event.keyCode>57 ||
window.event.keyCode==47) && window.event.keyCode!=45)
window.event.returnValue=false;
if( window.event.keyCode==46 && objectname.value.indexOf(".")>-1)
window.event.returnValue=false;

}

//验证不能为空的数据//
function gf_mandatory(as_prompt)
{
//as_prompt:tbx_a,不能为空*tbx_b,不能为空*
var fs_src = as_prompt;
var fas_p = fs_src.split("*");
var fas_n;
for(var i=0;i<fas_p.length;i++)
{
if (fas_p[i]!="")
{
fas_n = fas_p[i].split(",");
if (fas_n.length<2){alert("验证表达式有误,请检查");return false;}
if (document.all(fas_n[0])==null){alert("Control Name:" + fas_n[0]
+ " is wrong!");return false;}
if (gf_trim(document.all(fas_n[0]).value)=="")
{
alert(fas_n[1]);
document.all(fas_n[0]).focus();
return false;
}
}
}
return true;

}

//检查是否是数字,小数
function gf_chknum(ai_num)
{
var fi_i,fi_j,fi_dot,strTemp;
strTemp=".0123456789";
fi_dot = 0; //小数点个数
if (ai_num.toString().length == 0) return false;
if (ai_num.charAt(0) == "." || ai_num.charAt(ai_num.length - 1) == ".")
return false;
for(fi_i=0;fi_i<ai_num.length;fi_i++)
{
fi_j = strTemp.indexOf(ai_num.charAt(i));
if (fi_j == -1) return false;
if (ai_num.charAt(i) == ".")
{
fi_dot++;
if (fi_dot>1) return false;
}
}
return true;

}

//去除字符串的空格
function gf_trim(as_string)
{
while(as_string.length > 0 && as_string.indexOf(" ")==0) as_string =
as_string.substr(1);
while(as_string.length > 0 && as_string.lastIndexOf("
")==(as_string.length-1)) as_string =
as_string.substr(0,as_string.length-1);
return as_string;

}

//只能输入大写字符
function gf_lc2uc()
{
if (window.event.keyCode>=97 & window.event.keyCode<=122 )
{
window.event.keyCode = window.event.keyCode - 32;
}

}

/**
格式化日期函数
D--日期2004-12-25
DT--日期时间2004-12-25 12:05:56
**/
function gf_formatdate(as_date,as_datetype)
{
//2003-11-12 11:25:13
var fs_tmp = new Array();
var fs_date = (as_date==null?"":gf_trim(as_date));
var fs_datetype = (as_datetype==null?"D":"DT");
if (fs_date=="") return fs_date;
var fi_space = fs_date.indexOf(" ");
var fs_ymd = fs_date.substr(0,fi_space);
var fs_hms = fs_date.substr(fi_space+1);
if (fs_ymd == "") {fs_ymd = fs_hms;fs_hms = "";}
fs_tmp = fs_ymd.split("-");
fs_ymd = (fs_tmp[0].length==2?("20" + fs_tmp[0]):fs_tmp[0]) + "-" +
gf_AppendZero(fs_tmp[1]) + "-" + gf_AppendZero(fs_tmp[2]);
if (fs_hms != "")
{
fs_tmp = fs_hms.split(":");
fs_hms = " " + gf_AppendZero(fs_tmp[0]) + ":" + gf_AppendZero(fs_tmp[1])
+ (fs_tmp.length==3?(":" + gf_AppendZero(fs_tmp[2])):"");
}
fs_date = fs_ymd + (fs_datetype=="D"?"":fs_hms);
return fs_date;

}

//集装箱箱号验证
//Author: sundy 20003-4-28
//格式:gf_chkcntrno(as_cntrno, fi_choice ) return( boolean )
//功能:验证集装箱箱号:
//参数:
// as_cntrno 是否符合国际标准,
//返回值:True 符合国际标准或强行通过(特殊箱号)
//举例:gf_chkcntrno( 'TEXU2982987', 0 )
function gf_chkcntrno(as_cntrno,ai_choice)
{
var fi_ki;
var fi_numsum;
var fi_nummod;
var fai_num = new Array(11);
var fb_errcntrno=false;

if (as_cntrno==null) return true; //null不进行验证
if (gf_trim(as_cntrno)=="") return true; //空不进行验证

if (as_cntrno.length == 11) //国际标准为11位,最后一位是校验位,若不是11位肯定不是标准箱
{ for(fi_ki=1;fi_ki<=11;fi_ki++)
fai_num[fi_ki] = 0;
for(fi_ki=1;fi_ki<=4;fi_ki++) //根据国际标准验证法则处理箱号前面的4个英文字母

{
fch_char=as_cntrno.charAt(fi_ki-1).toUpperCase();
switch(true)
{ case (fch_char=="A"):{fai_num[fi_ki] = 10;break;}
case (fch_char>="V" && fch_char<="Z"):{fai_num[fi_ki] =
fch_char.charCodeAt() - 52;break;}
case (fch_char>="L" && fch_char<="U"):{fai_num[fi_ki] =
fch_char.charCodeAt() - 53;break;}
default:{fai_num[fi_ki] = fch_char.charCodeAt() - 54;break;}
}
}
for(fi_ki=5;fi_ki<=11;fi_ki++)
{ fch_char=as_cntrno.charAt(fi_ki-1);
fai_num[fi_ki] = parseInt(fch_char); //ctype((mid(as_cntrno, fi_ki, 1)),
integer)
}
fi_numsum = 0

for(fi_ki=1;fi_ki<=10;fi_ki++)
{
fi_sqr = 1;
for(i=1;i<fi_ki;i++){fi_sqr *=2;}
fi_numsum += fai_num[fi_ki] * fi_sqr;
}

if (as_cntrno.substr(0,4) == "HLCU") fi_numsum = fi_numsum - 2; //hapaq
lloyd的柜号与国际标准相差2
fi_nummod = fi_numsum % 11;
if (fi_nummod == 10) fi_nummod = 0;
if (fi_nummod == fai_num[11]) fb_errcntrno = true;
return fb_errcntrno;
}else{
return fb_errcntrno;
}

}

/*由于在Js中Replace只能替换一次,下面函数能全局替换2003.5.13 -sundy
as_expression --包含要替换的字符串
as_find --搜索的子字符串
as_replacement --要替换的字符串
替换字符串函数,如:"'"转换为"''"
*/
function gf_replace(as_expression,as_find,as_replacement)
{
var fs_expression = as_expression;
var fs_find = as_find;
var fs_replacement = as_replacement;
if (fs_expression == "") return "";
if (fs_find == "") return "";
//构造正则表达式\,$,(,),*,+,.,[,?,{,^,|为特殊字符,必须转义后替换
fs_regx1 = /\\/gi;
fs_find = fs_find.replace(fs_regx1,"\\\\");
fs_replacement = fs_replacement.replace(fs_regx1,"\\");

fs_regx1 = /\$/gi;
fs_find = fs_find.replace(fs_regx1,"\\\$");
fs_replacement = fs_replacement.replace(fs_regx1,"\$");
fs_regx1 = /\(/gi;
fs_find = fs_find.replace(fs_regx1,"\\\(");
fs_replacement = fs_replacement.replace(fs_regx1,"\(");
fs_regx1 = /\)/gi;
fs_find = fs_find.replace(fs_regx1,"\\\)");
fs_replacement = fs_replacement.replace(fs_regx1,"\)");
fs_regx1 = /\*/gi;
fs_find = fs_find.replace(fs_regx1,"\\\*");
fs_replacement = fs_replacement.replace(fs_regx1,"\*");
fs_regx1 = /\+/gi;
fs_find = fs_find.replace(fs_regx1,"\\\+");
fs_replacement = fs_replacement.replace(fs_regx1,"\+");
fs_regx1 = /\./gi;
fs_find = fs_find.replace(fs_regx1,"\\\.");
fs_replacement = fs_replacement.replace(fs_regx1,"\.");
fs_regx1 = /\[/gi;
fs_find = fs_find.replace(fs_regx1,"\\\[");
fs_replacement = fs_replacement.replace(fs_regx1,"\[");
fs_regx1 = /\?/gi;
fs_find = fs_find.replace(fs_regx1,"\\\?");
fs_replacement = fs_replacement.replace(fs_regx1,"\?");
fs_regx1 = /\^/gi;
fs_find = fs_find.replace(fs_regx1,"\\\^");
fs_replacement = fs_replacement.replace(fs_regx1,"\^");
fs_regx1 = /\{/gi;
fs_find = fs_find.replace(fs_regx1,"\\\{");
fs_replacement = fs_replacement.replace(fs_regx1,"\{");
fs_regx1 = /\|/gi;
fs_find = fs_find.replace(fs_regx1,"\\\|");
fs_replacement = fs_replacement.replace(fs_regx1,"\|");
fs_find = "/" + fs_find + "/gi";

//返回替换后的值
return fs_expression.replace(eval(fs_find),fs_replacement);

}

/*Jun.14,2003--sundy
字符串转换为数字(""-->0)as_type--str,num
参数说明:as_str--转换的字符串
ai_digit--转换的小数位数(null--不限制小数位数,0--转换为整型,>0按小数位数转换)
as_type--转换后返回的类型(null,"num"--转换为数字类型,"str"--转换为字符串(按小数格式化后的字符串)
例如:
gf_str2float("10.2124568795")返回float类型10.2124568795
gf_str2float("10.6",0)返回Int类型11(使用四舍五入的方法)
gf_str2float("10.2",2)返回float类型10.1
gf_str2float("10.2",2,"str")返回String类型"10.20"(按小数位数格式化字符串)
gf_str2float("10.216",2)返回float类型10.22
gf_str2float("10.216",2,"str")返回String类型"10.22"
*/
function gf_str2float(as_str,ai_digit,as_type)
{
var fdb_tmp = 0;
var fi_digit = 0;
var fs_digit = "1";
var fs_str = "" + as_str;
var fs_tmp1 = "";
var fs_tmp2 = "";
var fi_pos = 0;
var fi_len = 0;
fdb_tmp = parseFloat(isNaN(parseFloat(fs_str))?0:fs_str);

switch (true)
{
case (ai_digit==null)://不改变值,只转换为数字
fdb_tmp = fdb_tmp;
break;
case (ai_digit==0)://取得整数
fdb_tmp = Math.round(fdb_tmp);
break;
case (ai_digit>0)://按照传入的小数点位数四舍五入取值
for (var i=0;i<ai_digit;i++) fs_digit +="0";
fi_digit = parseInt(fs_digit);
fdb_tmp = Math.round(fdb_tmp * fi_digit) / fi_digit;
if (as_type=="str")
{
fs_tmp1 = fdb_tmp.toString();
fs_tmp1 +=((fs_tmp1.indexOf(".")!=-1)?"":".") +
fs_digit.substr(1);
fi_pos = fs_tmp1.indexOf(".") + 1 + ai_digit;
fdb_tmp = fs_tmp1.substr(0,fi_pos);
}
break;
}
return fdb_tmp;

}

//打印数据
function gf_dgdprint(as_reportname,as_where,as_subtitle,as_footer)
{ //打印datagrid --sherry 2003-08-19
/*
as_reportname--报表格式名 不能为空
as_where--where 提取条件,形如" From cod_Driver Where Corp ='本单位' 不能为空

as_subtitle --表头小标题
as_footer--表尾,
*/
var fs_Rpt_Property="width=" + (screen.width - 10) + ",height=" +
(screen.height - 55) + ",left=0,top=0";
if (as_reportname==null) as_reportname="";
if (as_where==null) as_where="";
if (as_subtitle==null) as_subtitle="";
if (as_footer==null) as_footer="";
var S_Rpt_ReportName=escape(as_reportname) ;
var S_Rpt_Where=escape(as_where) ;
var S_Rpt_SubTitle=escape(as_subtitle) ;
var S_Rpt_Footer=escape(as_footer) ;
var fs_parm = "";
fs_parm += "?S_Rpt_ReportName=" + S_Rpt_ReportName ;
fs_parm += "&S_Rpt_Where=" + S_Rpt_Where ;
fs_parm += "&S_Rpt_SubTitle=" + S_Rpt_SubTitle ;
fs_parm += "&S_Rpt_Footer=" + S_Rpt_Footer ;
window.open( "../SysPublic/sys_printer.aspx" +
fs_parm,"sys_printer",fs_Rpt_Property );

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值