[原创]我的Javascript(一)

转载请注明出处
及其作者feng_sundy


自己常用到的函数

//检测文件扩展名
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<loadTimes) {
  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("<font face='Arial' color='red' size='4'>Loading.....</font>");
    }
    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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值