现在的位置: 首页 > 综合 > 正文

公用js

2012年06月07日 ⁄ 综合 ⁄ 共 8093字 ⁄ 字号 评论关闭
document.getParentObjectByClassName=function(element,css){
var returnElement=element;
var tag=null;
while(!e.hasClassName(returnElement,css) && tag!="BODY"){
returnElement=(returnElement.parentNode || returnElement.parentElement);
if (returnElement.tagName){tag=returnElement.tagName;}
}
return(returnElement==element?null:returnElement);
};
document.Event=function(){
if(document.all) return(window.event);
var Fun=document.Event.caller;
while(Fun!=null){
var Arg=Fun.arguments[0];
if(Arg){
if((Arg.constructor==Event || Arg.constructor ==MouseEvent) || (typeof(Arg)=="object" && Arg.preventDefault && Arg.stopPropagation)) return(Arg);
}
Fun=Fun.caller;
}
Arg=Fun=null;
return(Arg);
};
document.This=function(){
return(document.Event().srcElement || document.Event().target);
};

 

function isArray(variable){
if (variable==null) return(false);
var reValue=true;
try {
reValue=(typeof(variable)=="object"&&variable.constructor==Array)?true:false;
}
catch(para) {
try{
variable.push(1);variable.pop();reValue=true;
}
catch(para){
reValue=false;
}
}
return(reValue);
};
function chkDate(s){
var reg = /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/;
//var reg1=/^(\d{4})([-])(\d{2})([-])(\d{2})/;//yyyy-mm-dd
//var reg2=/^(\d{4})([\/])(\d{2})([\/])(\d{2})/;//yyyy/mm/dd
return(reg.test(s));
}
function isFunction(fun) {
if (fun==null) return(false);
return (typeof(fun)=='function'&&fun.constructor==Function);
};
function isString(str){
if (str==null) return(false);
return (typeof(str)=='string'&&str.constructor==String);
};
function isNumber(obj){
if (obj==null) return(false);
return (typeof(obj)=='number'&&obj.constructor==Number);
};
function isDate(obj){
if (obj==null) return(false);
return (typeof(obj)=='object'&&obj.constructor==Date);
};
function isObject(obj){
if (obj==null) return(false);
return (typeof(obj)=='object'&&obj.constructor==Object);
};
function isNull(a) {
if (isArray(a)){return(a.length==0);}
return(a==null || (!a&&a!=0) || (isString(a)&&(a.replace(/(^\s*)|(\s*$)/g, "")=="" || a.toLowerCase()=="null" || a.toLowerCase()=="empty")) || typeof(a)=='undefined');
};
function isBoolean(a){
if (a==null) return(false);
return(typeof(a) == 'boolean'&&a.constructor==Boolean);
};
function $tag(parentElement,tagstring){//getElementsByTagName
var element=parentElement,tags=tagstring,temp=[],tmp;
element=$(element);
if (isNull(element)) element=document;
if (isNull(tags)) tags="*";
try{
tmp=element.getElementsByTagName(tags);
}
catch(para){
tmp=[];
}
for (var i=0; i<tmp.length; i++) {
temp.push(tmp[i])
}
tmp=null;
return(temp);
};
function chkTelephone(Tel)
{
var i,j,strTemp;
strTemp="0123456789-+#()";
if (Tel.length < 7 ){
return 0;
}
for (i=0;i<Tel.length;i++){
j=strTemp.indexOf(Tel.charAt(i));
if (j==-1){
return 0;
}
}
return 1;
}

function chkMobile(m){
var mobile=m;
var reg0=/^13\d{9}$/; //130--139。至少7位
var reg1=/^15\d{9}$/; //联通150-159。至少7位
var my=false;
if (reg0.test(mobile))my=true;
if (reg1.test(mobile))my=true;
return my;
}


function chkEmail(a){
if(!a.match(/^[\w\.\-]+@([\w\-]+\.)+[a-z]{2,4}$/ig)){
return false;
}
return true;

}
function chkNumeric(NUM){
var i,j,strTemp;
var PointNum;
PointNum=0
strTemp="0123456789.";
if(NUM=="")return 0;
for (i=0;i<NUM.length;i++){
j=strTemp.indexOf(NUM.charAt(i));
if (j==-1){
return 0;
}
if(j==10){
PointNum=PointNum+1;
}

}
if(PointNum>1){
return 0;
}
return 1;
}

function chkInteger(NUM){
return(/^[0-9]+$/.test(NUM));
}

function DrawImage(ImgD,x,y){
var flag=false;
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= x/y){
if(image.width>x){
ImgD.width=x;
ImgD.height=(image.height*x)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
//ImgD.alt=image.width+"x"+image.height;
}
else{
if(image.height>y){
ImgD.height=y;
ImgD.width=(image.width*y)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
//ImgD.alt=image.width+"x"+image.height;
}
}
}

function chkImageSize(ImgD,x,y){
var image=new Image();
image.src=ImgD;
if(image.width>x || image.height>y){
alert("图片的宽不能大于"+x+"像素;高不能大于"+y+"像素.");
return false;
}
return true;
}


// 是否有效的扩展名
function IsExt(url, opt){
var sTemp;
var b=false;
if(url==""){
return b;
}
var s=opt.toUpperCase().split("|");
for (var i=0;i<s.length ;i++ ){
sTemp=url.substr(url.length-s[i].length-1);
sTemp=sTemp.toUpperCase();
s[i]="."+s[i];
if (s[i]==sTemp){
b=true;
break;
}
}
return b;
}

function chkLength(strTemp){
var i,sum;
sum=0;
for(i=0;i<strTemp.length;i++){
if ((strTemp.charCodeAt(i)>=0) && (strTemp.charCodeAt(i)<=255))
sum=sum+1;
else
sum=sum+2;
}
return sum;
}

 

function jtrim(str)
{
while ((str.charAt(0)==" ")||(str.charAt(0)==" ")){
str=str.substr(1);
}
while (str.charAt(str.length-1)==" "){
str=str.substr(0,str.length-1);
}
return(str);
}


//设置选中
function checkAll(obj,value){
var arr=$A(document.getElementsByName(obj));
arr.each(function(e,i){$(e).checked=value;})
}

//至少有一个元素被选择
function checkSub(theform,theid){
var k=0;
for(var i=0;i<theform.elements.length;i++){
var e = theform.elements[i];
if (e.name == theid){
if(e.checked==true){
k++;
}
}
}
return k;
}
function goUrl(url){
window.location.replace(url);
}
function openUrl(url){
window.open(url,'');
}
var openwin=function(){
var me=this;
me.para={win:window,width:300,height:300,reobj:"",type:"value",title:"Open Window Test"};
this.set=function(){
var property,Arg=arguments[0];
if(typeof(Arg)=="object"){
for (property in Arg) {
me.para[property] = Arg[property];
}
}
else{
return;
}
};
this.open=function(url){
url=(url.indexOf("?")!=-1)?url+"&":url+"?";
var aw=parseInt((screen.availWidth/2)-(me.para.width/2));
var ah=parseInt((screen.availHeight/2)-(me.para.height/2));
if (document.all){
var retValues=window.showModalDialog(url+"title="+me.para.title,me.para.win, "dialogWidth:"+me.para.width+"px; dialogHeight:"+me.para.height+"px; dialogLeft:"+aw+"px; dialogTop:"+ah+"px; status:no; directories:no; menubar:no; scrollbars:no; Resizable:no; location:no;toolbar:no;");
if (me.para.type=="value"){
document.getElementById(me.para.reobj).value=retValues;
}
else{
if (me.para.type=="html" || me.para.type=="text"){
document.getElementById(me.para.reobj).innerHTML=retValues;
}
else{
try{
eval(me.para.reobj+"('"+retValues+"')");
}
catch(e){
alert(e.description);
}
}
}
retValues=null;
//IE End
}
else{
window.open(url+"return="+me.para.reobj+"&type="+me.para.type+"&title="+me.para.title, me.para.win, "top="+aw+",left="+ah+",scrollbars=no,dialog=yes,modal=yes,width="+me.para.width+",height="+me.para.height+",resizable=no,location=no,menubar=no,toolbar=no,status=no");
}
aw=null;
ah=null;
};
};

function showlayer(layer_name)
{
var hidden_layer = document.getElementById(layer_name);
hidden_layer.style.display=hidden_layer.style.display==""?"none":"";
}

function ShowAllChannel(_this,id,bgleft,bgtop1,bgtop2){
document.getElementById(id).style.display="block";
var bgpos1 = bgleft + " " + bgtop1;
var bgpos2 = bgleft + " " + bgtop2;
document.getElementById(id).onmouseover=function(){this.style.display="block";_this.style.backgroundPosition=bgpos1;}
document.getElementById(id).onmouseout=function(){this.style.display="none";_this.style.backgroundPosition=bgpos2;}
_this.style.backgroundPosition=bgpos1;
_this.onmouseout=function(){document.getElementById(id).style.display="none";_this.style.backgroundPosition=bgpos2;}
}

window.onerror=function (){
return true;
}

function checksform(form){
String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,"")}
if((form.skey.value=="") || (form.skey.value.substring(0,3)=='请输入'))
{
alert("请输入您要搜索的关键词!");
return false;
}
return true;
}

//控制页面广告后载
function endAdv(str1,str2,initheight){
document.getElementById(str1).style.height = initheight + "px";
document.getElementById(str1).innerHTML = $(str2).innerHTML;
document.getElementById(str2).innerHTML = "";
}

抱歉!评论已关闭.