Javascript通用函数

  1 window.onerror  =  ScriptErrorReport;
  2 // window.onerror = ScriptErrorKill;
  3
  4 //  ** 脚本错误报告 ** //
  5 function  ScriptErrorReport(msg, surl, lineno)
  6 {
  7    var strMsg = ("抱歉,网页在运行过程中出现脚本错误!\n"
  8          + "\n错误信息:" + msg
  9          + "\n所在行:" + lineno
 10          + "\n\n请尝试刷新页面!\n\n如果问题仍然存在,请与系统管理员联系!");
 11    alert(strMsg);
 12    return true;
 13}

 14
 15 //  ** 无脚本错误提示 ** //
 16 function  ScriptErrorKill()
 17 {
 18    return true;
 19}

 20
 21 //  ** 向window.load添加函数 ** //
 22 function  WindowLoadEventAdd(fun)
 23 {
 24    var fnLoad = window.onload;
 25    if(typeof(fnLoad) != "function")
 26    {
 27        window.onload = fun;
 28    }

 29    else
 30    {
 31        window.onload = function ()
 32        {
 33            fnLoad();
 34            fun();
 35        }

 36    }

 37}

 38
 39 //  ** 获取控件位置 ** //
 40 function  getObjectPosition(obj)
 41 {
 42    var left = 0;
 43    var top = 0;
 44    var width = 0;
 45    var height = 0;
 46    
 47    if(typeof(obj) != "undefined")
 48    {
 49        var parent = obj;
 50        while(parent != document.body)
 51        {
 52            left += parent.offsetLeft;
 53            top += parent.offsetTop;
 54            if(parseInt(parent.currentStyle.borderLeftWidth) > 0)
 55            {
 56                left += parseInt(parent.currentStyle.borderLeftWidth);
 57            }

 58            if(parseInt(parent.currentStyle.borderTopWidth) > 0)
 59            {
 60                top += parseInt(parent.currentStyle.borderTopWidth);
 61            }

 62            parent = parent.offsetParent;
 63        }

 64        
 65        width = obj.offsetWidth;
 66        height = obj.offsetHeight;
 67    }

 68    
 69    return new Array(left, top, width, height);
 70}

 71
 72
 73 //  ** 打开新的窗口 ** //
 74 function  WindowOpen(URL, Width, Height, Left, Top)
 75 {
 76    if(!Left > 0{ Left = 100;}
 77    if(!Top > 0{ Right = 100; } 
 78    if(!Width > 0{ Width = 650; }
 79    if(!Height > 0{ Height = 500; }
 80    
 81    var strSetting = "width=" + Width + ",height=" + Height + ",left=" + Left + ",top=" + Top
 82                   + ",toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no";
 83    window.open(URL,"",strSetting)
 84}

 85
 86 //  ** 打开模式对话框 ** //
 87 function  ModalDialogShow(URL, Width, Height)
 88
 89    if(!Width > 0{ Width = 650; }
 90    if(!Height > 0{ Height = 500; }
 91    
 92    var strSetting = "dialogWidth:" + Width + "px;dialogHeight:" + Height 
 93                   + "px;center:yes;status:no;scroll:no;help:no;resizable:yes";
 94    
 95    var varReturn = window.showModalDialog(URL,"",strSetting);
 96    return varReturn;
 97}

 98
 99 //  ** 打开非模式对话框 ** //
100 function  ModelessDialogShow(URL, Width, Height)
101
102    if(!Width > 0{ Width = 650; }
103    if(!Height > 0{ Height = 500; }
104    
105    var strSetting = "dialogWidth:" + Width + "px;dialogHeight:" + Height 
106                   + "px;center:yes;status:no;scroll:no;help:no;resizable:yes";
107    
108    window.showModelessDialog(URL,"",strSetting);
109}

110
111 // 图片按比例缩放
112 var  flag = false ;
113 function  resizeimg(ImgD)
114 {
115    var image=new Image();
116    var iwidth = 500//定义允许图片宽度
117    var iheight = 380//定义允许图片高度
118    image.src=ImgD.src;
119    if(image.width>0 && image.height>0)
120    {
121        flag=true;
122        if(image.width/image.height>= iwidth/iheight)
123        {
124            if(image.width>iwidth)
125            
126                ImgD.width=iwidth;
127                ImgD.height=(image.height*iwidth)/image.width;
128            }

129        else
130        {
131            ImgD.width=image.width; 
132            ImgD.height=image.height;
133        }

134        ImgD.alt=image.width+"×"+image.height;
135        }

136        else
137        {
138        if(image.height>iheight)
139        
140            ImgD.height=iheight;
141            ImgD.width=(image.width*iheight)/image.height; 
142        }
else
143        {
144            ImgD.width=image.width; 
145            ImgD.height=image.height;
146        }

147        ImgD.alt=image.width+"×"+image.height;
148        }

149        }

150}
 
151
152      // 重新登录
153      function  ReLogin()
154      {
155         window.parent.exit=false;
156         alert("由于您的登录信息已过期,系统需要重新登录!");
157         window.parent.location.href = "http://www.cnblogs.com/login.aspx?loginState=Relogin";
158    }

159     
160      // 键盘控制
161      function  CheckKey()
162      {
163        if(event.keyCode==13)
164        {
165            event.keyCode=9;
166        }

167    }

168     
169      // 引入气泡提示JS文件
170      function  initScript(url)
171      {        
172        var scriptstyle = document.createElement("script");
173        scriptstyle.setAttribute("language","javascript");
174        scriptstyle.setAttribute("type","text/javascript");        
175        scriptstyle.setAttribute("src",url);    
176        document.getElementsByTagName("head")[0].appendChild(scriptstyle);
177        alert("dd");
178    }
  1 // 页面加载时的提示
  2      // by HambyWu
  3      // 2008-05-11
  4      function  showloading()
  5      {
  6    document.getElementById('ifrmBack').style.display='block';
  7    document.getElementById('divPopup').style.display='block';
  8    }

  9     
 10      function  loadclick()
 11      {
 12        document.getElementById("btnload").click();
 13    }

 14     
 15      // 设置页面上所有textbox和dropdownlist的onkeydown
 16      // 响应回车事件,变为Tab
 17      // By Guanyb
 18      // 2007-11-13
 19      function  findControlAllSetTab()
 20      {
 21        var inputs=document.getElementsByTagName("input");
 22        var drop=document.getElementsByTagName("SELECT");
 23        for(j=0;j<inputs.length;j++)
 24        {
 25            if(inputs[j].type=="text")
 26            {
 27                inputs[j].onkeydown=function()
 28                {
 29                    if(event.keyCode==13)
 30                    {
 31                        event.keyCode=9;
 32                    }

 33                }

 34            }

 35        }

 36        for(i=0;i<drop.length;i++)
 37        {
 38            if(drop[i].tagName.toUpperCase()=="SELECT")
 39            {
 40                drop[i].onkeydown=function()
 41                {
 42                    if(event.keyCode==13)
 43                    {
 44                        event.keyCode=9;
 45                    }

 46                }

 47            }

 48        }

 49    }

 50     
 51      // 为每个下拉列表框应用样式
 52      function  setStyle_select()
 53      {
 54        var drop=document.getElementsByTagName("SELECT");
 55        for(var i=0;i<drop.length;i++)
 56        {
 57            if(!!drop[i])
 58            {
 59                drop[i].onfocus= function()
 60                {
 61                    fos(this);
 62                }

 63                drop[i].onblur=function()
 64                {
 65                    blu(this);
 66                }

 67            }

 68        }

 69    }

 70     
 71      // 为每个文本框应用样式
 72      function  setStyle_input()
 73      {
 74        var drop=document.getElementsByTagName("input");
 75        var txt=document.getElementsByTagName("textarea");
 76        for(var i=0;i<drop.length;i++)
 77        {
 78            if(!!drop[i])
 79            {
 80                if(drop[i].type=="text")
 81                {
 82                    
 83                    //if(drop[i].id.toUpperCase().indexOf("DATE")==-1)
 84                    //{
 85                    drop[i].onfocus= function()
 86                    {
 87                        fos(this);
 88                    }

 89                    drop[i].onblur=function()
 90                    {
 91                        blu(this);
 92                    }

 93                    //}
 94                }

 95            }

 96        }

 97        for(var i=0;i<txt.length;i++)
 98        {
 99            if(!!txt[i])
100            {
101                if(txt[i].type=="textarea")
102                {
103
104                    txt[i].onfocus= function()
105                    {
106                        fos(this);
107                    }

108                    txt[i].onblur=function()
109                    {
110                        blu(this);
111                    }

112                }

113            }

114        }

115    }

116     
117 // 得到焦点文本的颜色
118 function   fos(obj)
119 {
120    obj.style.backgroundColor='#ffffcc';
121}

122
123 // 失去焦点文本的颜色
124 function   blu(obj)
125 {
126    obj.style.backgroundColor='#ffffff' ;
127}
     
128
129 function  GetFocus(obj)
130 {
131    var con = document.getElementById(obj);
132    if(!!con)
133    {
134        con.focus();
135        return true;
136    }

137    else
138    {
139        alert("对象不能为空!---setFocus");
140        return false;
141    }

142}

143
144 // 使光标在文本的后面
145 function  SetTextBehind(obj)
146 {
147    if(!!obj)
148    {
149        var text=obj.createTextRange();
150        text.collapse(false);
151        text.select();
152        return true;
153    }

154}

155
156 //
157 function  AutoInput(obj)
158 {
159    if(!!obj)
160    {
161        if(obj.value.trim().length==0)
162        {
163            obj.value="粤A";
164            SetTextBehind(obj);
165        }

166    }

167}

168
169                 
170 function  parentReload()
171   {
172    window.dialogArguments.location.href=window.dialogArguments.location.href;
173 }

174
175 function  objUpper(obj)
176 {
177    if(!!obj)
178    {
179        //屏蔽标点符号
180        var str=/[.;:'",~!@#$%^&*()+|\\=/?><,。;‘《》?“:!·#¥%……—*()、]/g;
181        obj.value=obj.value.toUpperCase().replace(str,'');
182        return true;
183
184    }

185}

186
187 function  objFloat(obj)
188 {
189    if(!!obj)
190    {
191        if(isNaN(obj.value))
192        {
193            obj.value="";
194        }

195    }

196}

197
198 function  objInt(obj)
199 {
200    if(!!obj)
201    {
202        obj.value=obj.value.replace(/[^\d]/g,'');
203        return true;
204    }

205}
        
206
207 // 判断机动车登记日期的合法性
208 function  ValidDate(sDate,AddYear)
209 {
210    var d=sDate.replace(/\-/g,"/") ;
211    var t=new Date(d);
212    var n=new Date();
213    n.setFullYear(n.getFullYear()+AddYear);
214    if(t>n)
215    {
216        return false;
217    }

218    return true;
219}

220             
221   // 判断日期有效性
222 function  isValidDate(dateStr) 
223 {
224    try
225    {
226        var r=dateStr.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
227        if (r==null)
228        {
229            return false;
230        }

231        var d = new Date(r[1],r[3]-1,r[4]); 
232        return (d.getFullYear()==r[1&& (d.getMonth()+1)==r[3&& d.getDate()==r[4]);
233    }

234    catch(e)
235    {
236        return false;
237    }

238}

239
240 // 验证中文和全角符号
241 function  HaveChinese(s)
242 {   
243    if   (s==null)
244        return   true;   
245    if   (s.length==0)
246        return   true;   
247    var   reg   =   /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi;   
248    if   (reg.test(s))
249    {   
250        return   false;   
251    }
   
252        return   true;   
253  }

254   
255 // 去掉字符串前后空格
256 function  trim(s)
257 {   
258  return s.replace(/(^\s*)|(\s*$)/g,"");   
259}
     
260
261 function  trim1(myStr)
262 {
263    return ltrim(rtrim(myStr));
264}

265
266 String.prototype.trim = function ()
267
268    return this.replace(/(^\s*)|(\s*$)/g, "");
269}

270
271 // 去掉myStr左边的空格
272 function  ltrim(myStr)
273 {
274    var strTemp;
275    strTemp = myStr;
276    while ((strTemp!="")&&(strTemp.substr(0,1)==" "))
277    {
278        strTemp = strTemp.substr(1,strTemp.length - 1);
279    }

280    return strTemp;
281}

282
283 // 去掉myStr右边的空格
284 function  rtrim(myStr)
285 {
286    var strTemp;
287    strTemp = myStr;
288    while ((strTemp!="")&&(strTemp.substr(strTemp.length -1,1)==" "))
289    {
290        strTemp = strTemp.substr(0,strTemp.length - 1);
291    }

292    return strTemp;
293}

294         
295 function  regInput(obj, reg, inputStr)
296 {
297    var docSel    = document.selection.createRange()
298    if (docSel.parentElement().tagName != "INPUT")    
299    return false
300    oSel = docSel.duplicate()
301    oSel.text = ""
302    var srcRange    = obj.createTextRange()
303    oSel.setEndPoint("StartToStart", srcRange)
304    var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length)
305    return reg.test(str)
306}
  1 // --------------------------------------------
  2 // 等待提示
  3 // 2008-05-12
  4 // By HambyWu
  5 // --------------------------------------------
  6 // 初始化,在页面加载时调用
  7 function  initWaiting(left, top, weight, height)
  8 {
  9    if(isNaN(left))
 10        left = 50;
 11    if(isNaN(top))
 12        top = 10;
 13    if(isNaN(weight))
 14        weight = 200;
 15    if(isNaN(height))
 16        height = 30;
 17    
 18    var style = "background: #FFFFE1; border: 1px solid #A9D3F2; filter: alpha(opacity=90);" +
 19                "position: absolute; left: " + left + "px; top: " + top + "px; width: " + weight + "px; height: " + height + "px;" +
 20                "text-align: center; padding-top: 5px; display: none;";
 21                
 22    var text = "正在执行中,请稍候"
 23    
 24    var div = document.createElement("div");
 25    
 26    div.setAttribute("id","divWaiting");
 27    div.style.cssText = style;
 28       div.innerText = text;
 29    
 30    document.body.appendChild(div);
 31       
 32}

 33 // 显示,在按钮点击时调用
 34 function  showWaiting()
 35 {
 36    var div = document.getElementById('divWaiting');
 37    if(!!div)
 38        div.style.display = "block";
 39}

 40 // 隐藏
 41 function  hiddenWaiting()
 42 {
 43    var div = document.getElementById('divWaiting');
 44    if(!!div)
 45        div.style.display = "none";
 46}

 47
 48 // ----------------------------------------------
 49 //  JavaScript 验证函数
 50 // 2008-05-12
 51 // By HambyWu
 52 // ----------------------------------------------
 53
 54 //  ** 身份证号验证 ** //
 55 function  IDCardNumberCheck(str)
 56 {
 57    return (/^\d{15}(?:|(?:\d{3}|\d{2}X))$/.test(str));
 58}

 59
 60 //  ** 整数验证 ** //
 61 function  IntegerCheck(str)
 62 {
 63    return (/^\d+$/.test(str));
 64}

 65
 66 //  ** 小数验证 ** //
 67 function  NumericCheck(str)
 68 {
 69    return (/^-?\d+(\.\d+)?$/.test(str));
 70}

 71
 72 //  ** 短日期验证 ** //
 73 function  ShortDateCheck(str)
 74 {
 75  if (str.length == 0return true;
 76  var r = str.match(/^(\d{1,4})(-|\.)(\d{1,2})\2(\d{1,2})$/); 
 77  if(r==nullreturn false
 78  var d= new Date(r[1], r[3]-1, r[4]); 
 79  return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
 80}

 81
 82
 83 //  ** 产权证号/他项权证号 验证 ** //
 84 function  CerCodeCheck(str)
 85 {
 86  return (/^[A-Za-z]d{6}$/.test(str))
 87}

 88
 89
 90 // =============================================================
 91 // Array 对象 方法扩展
 92 // 2008-05-12
 93 // By HambyWu
 94 //
 95 // 数组 判断是否存在元素 方法
 96 if ( ! Array.prototype.has)
 97 {
 98    Array.prototype.has = function (val)
 99    {
100        var i;
101        for(i = 0; i < this.length; i++)
102        {
103            if(this[i] == val)
104            {
105                return true;
106            }

107        }

108        return false;
109    }

110}

111
112 // 数组 按索引移除元素 方法
113 if ( ! Array.prototype.removeAt)
114 {
115    Array.prototype.removeAt = function (index)
116    {
117        if(index >= 0 && index < this.length)
118        {
119            var i;
120            for(i = index; i < this.length - 1; i++)
121            {
122                this[i] = this[i + 1];
123            }

124            this.length -= 1;
125            return true;
126        }

127        else
128        {
129            return false;
130        }

131    }

132}

133
134 // 数组 按值移除元素 方法
135 if ( ! Array.prototype.removeVal)
136 {
137    Array.prototype.removeVal = function (val)
138    {
139        var i;
140        for(i = 0; i < this.length; i++)
141        {
142            if(this[i] == val)
143            {
144                return this.removeAt(i);
145            }

146        }

147        return false;
148    }

149}

150
151 // 数组 移除所有元素 方法
152 if ( ! Array.prototype.removeAll)
153 {
154    Array.prototype.removeAll = function()
155    {
156        this.length = 0;
157        return true;
158    }

159}

160
161 // 数组 寻找元素索引 方法
162 if ( ! Array.prototype.indexOf)
163 {
164    Array.prototype.indexOf = function(val)
165    {
166        var i;
167        for(i = 0; i < this.length; i++)
168        {
169            if(this[i] == val)
170            {
171                return i;
172            }

173        }

174        return -1;
175    }

176}

177 // ===============================================================
178
179

转载于:https://www.cnblogs.com/hambywu/archive/2008/05/22/1205006.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值