來公司後所在項目組開發的是一個用戶端的軟體,軟體介面採用WEB介面來體現,因此要用到不少Javascript.來公司後所在項目組開發的是一個用戶端的軟體,軟體介面採用WEB介面來體現,因此要用到不少Javascript.把自己寫的一些通用JS函數整理了下:)
/*判斷瀏覽器版本*/
var w3c=(document.getElementById)? true: false;
var agt=navigator.userAgent.toLowerCase();
var ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1) && (agt.indexOf("omniweb") == -1));
var ie5=(w3c && ie)? true : false;
var ns6=(w3c && (navigator.appName=="Netscape"))? true: false;
var op8=(navigator.userAgent.toLowerCase().indexOf("opera")==-1)? false:true;
function Ob(o){//取得一個對象
return document.getElementById(o)?document.getElementById(o):o;
}
function IsSubStr(p,s){//判斷是否為子字串
return (p.indexOf(s)>-1);
}
function Hd(o){//隱藏某個對象
Ob(o).style.display="none";
}
function Sw(o){//顯示某個對象
Ob(o).style.display="block";
}
function Sw2(o){//顯示某個對象
Ob(o).style.display="";
}
function Sf(o,s,b){
//該函數用於多按鈕共用同一表單,其中:o為表單的ID b為隱藏的input的ID(其value供後如判斷使用者提交動作類型) s為使用者提交動作類型
Ob(b).value=s;
Ob(o).submit();
}
function ExChgCls(o,a,b){//切換對象的className
o.className=o.className==a?b:a;
}
function ExChgHtml(o,a,b){//切換對象的innerHTML
o.innerHTML=o.innerHTML==a?b:a;
}
function Oh(o,s){ //輸出字串給某個對象
Ob(o).innerHTML=s;
}
function SetSrc(o,s){//改變img、iframe等對象的src值
Ob(o).src=s;
}
/*XmlHttp*/
function Gp(url,o){ //GetHttpPage
var o=Ob(o);
var PageRequest = false;
IsLoading(o);
if (window.XMLHttpRequest) {// if Mozilla, Safari etc
PageRequest = new XMLHttpRequest();
}
else if (window.ActiveXObject){ // if IE
try {
PageRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
PageRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){;}
}
}
else {return false ;}
PageRequest.onreadystatechange=function(){
if (PageRequest.readyState == 4) {
Lp(PageRequest,o);
}
}
PageRequest.open('GET', url, true);
PageRequest.send(null);
}
function Lp(PageRequest,o){ //LoadPage
if (PageRequest.readyState == 4 && (PageRequest.status==200 || window.location.href.indexOf("http")==-1))
Ob(o).innerHTML=PageRequest.responseText;
//Hd("OpMsg");
}
function IsLoading(o){
o.innerHTML="資料載入中,請稍候......";
//Sw2("OpMsg");
}
//Include Javascript File
function IncJs(sSrc,sID){
var oHead = document.getElementsByTagName('head')[0];
var oScript = document.createElement('script');
oScript.type = "text/javascript";
oScript.src = sSrc;
oScript.id = sID;
if(oScript.readyState=="loaded") {oHead.appendChild(oScript); }
}
/*Move & Remove*/
function Mv(a,b,n) {
var i = 0;
var f=Ob(a);
var t=Ob(b);
var n=n?n:0;
if (n==0) {
for(var ii=0; ii<f.options.length; ii++) {
if(f.options[ii].selected ) {
var no = new Option();
no.value = f.options[ii].value;
no.text = f.options[ii].text;
t.options[t.options.length] = no;
}
}
}
else {
for(var ii=0; ii<f.options.length; ii++) {
if(f.options[ii].selected ) {
t.value += ","+f.options[ii].value;
}
}
}
Rv(a);
}
function Rv(o) {
var o=Ob(o);
for(var ii=o.options.length-1; ii>=0; ii--) {
if(o.options[ii].selected && o.options[ii] != "") {
o.options[ii].value = "";
o.options[ii].text = "";
o.options[ii] = null;
}
}
}
function ChgInp(o1,o2){
Ob(o2).readOnly=(!Ob(o1).checked);
if (Ob(o1).checked) {
Ob(o2).className="InpTxt";
Ob(o2).focus();
} else {
Ob(o2).className="InpTxt0";
}
}
/*Checkbox*/
function DoChk(o,o2,s){
this.OldCls = s || "";
if (o.checked==false) {
o.parentNode.parentNode.className=this.OldCls;
Ob("BtnChkAll").checked=false;
} else {
o.parentNode.parentNode.className="chked";
IsAllChk(o2,this.OldCls);
}
}
function IsAllChk(o,s){
var ChkNums=0;
var arrObj=Ob(o).getElementsByTagName("input");
this.OldCls = s || "";
for (i=0;i<arrObj.length;i++ ) {
if (arrObj[ii].checked) {
ChkNums+=1;
arrObj[ii].parentNode.parentNode.className="chked";
} else {
arrObj[ii].parentNode.parentNode.className=this.OldCls;
}
}
//alert(ChkNums+":" +arrObj.length)
if (ChkNums==arrObj.length-1) {
Ob("BtnChkAll").checked=true;
} else {
Ob("BtnChkAll").checked=false;
}
}
function ChkAll(o,o2,s){
var arrObj=Ob(o2).getElementsByTagName("input");
this.OldCls = s || "";
for (ii=1;ii<arrObj.length;ii++ ) {
arrObj[ii].checked=o.checked;
if (o.checked) {
arrObj[ii].parentNode.parentNode.className="chked";
} else {
arrObj[ii].parentNode.parentNode.className=this.OldCls;
}
}
}
function OppChk(){
var arrObj=Ob("Photos").getElementsByTagName("input");
for (ii=0;ii<arrObj.length;ii++ ) {
arrObj[ii].checked=!arrObj[ii].checked;
}
IsAllChk();
}
/*Test Nums*/
function IsIntNum(s){
var reIntNum=/^\-?\d+$/;
return reIntNum.test(s);
}
function IsBetween(n0,n1,n2,t){
this.CompareType=t || 4;
switch (this.CompareType) {
case 0 :
return (n0>n1 && n0<n2);
case 1 :
return (n0>=n1 && n0<n2);
case 2 :
return (n0>n1 && n0<=n2);
default :
return (n0>=n1 && n0<=n2);
}
}