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 );

}
發佈了7 篇原創文章 · 獲贊 0 · 訪問量 1746
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章