常用javasript

  1. Javascript 判断字符长度  包括全角

function CheckLength(txtObj,length) {
    var str = txtObj.value;
    var len = str.length;
    var temp="";
    var reLen = 0;

    for (var i = 0; i < len; i++) {    
        if (str.charCodeAt(i) >255 ) {
            // 全角  
            reLen += 2;
        } else {
           reLen++;
        }
        if (reLen < length+1){
           temp+=str.charAt(i);
        }else{
           txtObj.value= temp;
        }
       
    }
}

  • ----------------Author Teng-------------  
  1. 验证是否为空  
  • function check_blank(obj, obj_name){  
  •       if(obj.value != ''){     
  •             return true;     
  •      }else{     
  •          alert(obj_name + "所填不能为空!");   
  •          obj.value = "";  
  •          return false;     
  •      }     
  • }  
  •   
  1. 过滤输入字符的长度  
  • function check_str_len(name,obj,maxLength){     
  •     obj.value=obj.value.replace(/(^\s*)|(\s*$)/g, "");  
  •     var newvalue = obj.value.replace(/[^\x00-\xff]/g, "**");   
  •     var length11 = newvalue.length;   
  •     if(length11>maxLength){  
  •         alert(name+"的长度不能超过"+maxLength+"个字符!");  
  •         obj.value="";  
  •         obj.focus();       
  •     }   
  •  }  
  •   
  1. 验证只能为数字  
  • function checkNumber(obj){  
  •     var reg = /^[0-9]+$/;  
  •     if(obj.value!=""&&!reg.test(obj.value)){  
  •         alert('只能输入数字!');  
  •         obj.value = "";  
  •         obj.focus();  
  •         return false;  
  •     }  
  • }  
  •   
  1. 验证数字大小的范围  
  •   
  • function check_num_value(obj_name,obj,minvalue,maxvalue){  
  •     var reg = /^[0-9]+$/;  
  •     if(obj.value!=""&&!reg.test(obj.value)){  
  •         alert(obj_name+'只能输入数字!');  
  •         obj.value = "";  
  •         obj.focus();  
  •         return false;  
  •     }else if(minvalue>obj.value||obj.value>maxvalue){  
  •         alert(obj_name+"的范围是"+minvalue+"-"+maxvalue+"!");  
  •         obj.value="";  
  •         obj.focus();  
  •         return false;  
  •     }  
  •   
  • }  
  •   
  1. 验证只能是字母和数字  
  • function checkZmOrNum(zmnum){  
  •   var zmnumReg=/^[0-9a-zA-Z]*$/;  
  • if(zmnum.value!=""&&!zmnumReg.test(zmnum.value)){  
  •      alert("只能输入是字母或者数字,请重新输入");  
  •      zmnum.value="";  
  •      zmnum.focus();  
  •      return false;  
  •   }  
  • }  
  •   
  1. 验证双精度数字  
  • function check_double(obj,obj_name){  
  •     var reg = /^[0-9]+(\.[0-9]+)?$/;  
  •     if(obj.value!=""&&!reg.test(obj.value)){  
  •         alert(obj_name+'所填必须为有效的双精度数字');  
  •         obj.value = "";  
  •         obj.focus();  
  •         return false;  
  •     }  
  • }  
  •      
  •   
  1. 复选框全选  
  • function checkboxs_all(obj,cName){  
  •     var checkboxs = document.getElementsByName(cName);  
  •     for(var i=0;i<checkboxs.length;i++){  
  •         checkboxs[i].checked = obj.checked;  
  •     }     
  • }  
  •   
  •   
  1. 验证邮政编码  
  • function check_youbian(obj){  
  •     var reg=/^\d{6}$/;   
  •     if(obj.value!=""&&!reg.test(obj.value)){  
  •         alert('邮政编码格式输入错误!');  
  •         obj.value = "";  
  •         obj.focus();  
  •         return false;  
  •     }  
  • }  
  •   
  1. 验证邮箱格式  
  • function check_email(obj){  
  •     var reg = /^[a-zA-Z0-9_-]+(\.([a-zA-Z0-9_-])+)*@[a-zA-Z0-9_-]+[.][a-zA-Z0-9_-]+([.][a-zA-Z0-9_-]+)*$/;   
  •     if(obj.value!=""&&!reg.test(obj.value)){  
  •         obj.select();  
  •         alert('电子邮箱格式输入错误!');  
  •         obj.value = "";  
  •         obj.focus();  
  •         return false;  
  •     }  
  • }  
  •   
  1. /*验证固定电话号码 
  •   0\d{2,3}   代表区号    
  •   [0\+]\d{2,3}   代表国际区号 
  •  \d{7,8} 代表7-8位数字(表示电话号码) 
  •  正确格式:区号-电话号码-分机号(全写|只写电话号码) 
  • */  
  •   
  • function check_phone(obj){  
  •     var reg=/^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;   
  •     if(obj.value!=""&&!reg.test(obj.value)){  
  •         alert('电话号码格式输入错误!');  
  •         obj.value = "";  
  •         obj.focus();  
  •         return false;  
  •     }  
  • }  
  •   
  1. 验证手机号码(检验13,15,18开头的手机号!)  
  • function check_telephone(obj){  
  •     var reg= /^[1][358]\d{9}$/;  
  •     if(obj.value!=""&&!reg.test(obj.value)){  
  •         alert('手机号码格式输入错误!');  
  •         obj.value = "";  
  •         obj.focus();  
  •         return false;  
  •     }  
  • }  
  •   
  1. 验证是否为中文  
  • function isChinese(obj,obj_name){  
  •     var reg=/^[\u0391-\uFFE5]+$/;   
  •     if(obj.value!=""&&!reg.test(obj.value)){  
  •         alert(obj_name+'必须输入中文!');  
  •         obj.value = "";  
  •         obj.focus();  
  •         return false;  
  •     }  
  • }  
  •   
  1. 判断是否是IE浏览器  
  •   
  • function checkIsIE(){  
  •     if(-[1,]){     
  •      alert("这不是IE浏览器!");     
  •     }else{     
  •      alert("这是IE浏览器!");     
  •     }   
  • }  
  •    
  1. 检验时间大小(与当前时间比较)  
  • function checkDate(obj,obj_name){  
  •     var obj_value=obj.value.replace(/-/g,"/");//替换字符,变成标准格式(检验格式为:'2009-12-10')  
  •     // var obj_value=obj.value.replace("-","/");//替换字符,变成标准格式(检验格式为:'2010-12-10 11:12')  
  •     var date1=new Date(Date.parse(obj_value));     
  •     var date2=new Date();//取今天的日期  
  •     if(date1>date2){  
  •         alert(obj_name+"不能大于当前时间!");  
  •         return false;  
  •     }  

16. 后退 前进

<input type="button" value="后退" onClick="history.go(-1)">

<input type="button" value="前进" onClick="history.go( 1 );return true;">

返回

<form><input type="button" value="返回上一步" onClick="history.back(-1)"></form>

17. 查看源码

<input type="button" name="view" value="查看源码" onClick="window.location="view-source:" +window.location.href">

18. 禁止查看源码

<body οncοntextmenu="return false"></body>

19. 刷新按钮一

<input type="button" value="刷新按钮一" onClick="ReloadButton()">

<script>function ReloadButton(){location.href="i001.htm";}</script>

刷新按钮二

<input type="button" value="刷新按钮二" onClick="history.go(0)">

20. 回首页按钮

<input type="button" value="首页" onClick="HomeButton()">

<script>function HomeButton(){location.href=http://www.winliuxq.com;}</script>

21. 弹出警告框

<input type="button" value="弹出警告框" onClick="AlertButton()">

<script>function AlertButton(){window.alert("要多多光临呀!");}</script>

22. 状态栏信息

<input type="button" value="状态栏信息" onClick="StatusButton()">

<script>function StatusButton(){window.status="要多多光临呀!";}</script>

23. 背景色变换

<form><input type="button" value="背景色变换" onClick="BgButton()"></form>

<script>function BgButton(){

if (document.bgColor=='#00ffff')

    {document.bgColor='#ffffff';}

else{document.bgColor='#00ffff';}

}

</script>

24. 打开新窗口

<input type="button" value="打开新窗口" onClick="NewWindow()">

<script>function NewWindow(){window.open("c01.htm","","height=240,width=340,status=no,location=no,toolbar=no,directories=no,menubar=no");}

</script>

25. 窗口最小化

<OBJECT id="min" type="application/x-oleobject" classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11"><PARAM name="Command" value="Minimize"></OBJECT><button onClick="min.Click()">窗口最小化</button>

26. 全屏代码

<input type="BUTTON" name="FullScreen" value="全屏显示" onClick="window.open(document.location, 'butong_net', 'fullscreen')">

27. 关闭窗口

<OBJECT id=closes type="application/x-oleobject" classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11"><param name="Command" value="Close"></object><input type="button" value="关闭窗口" onClick="closes.Click();">

关闭窗口

<input type=button value=关闭窗口 onClick="javascript:self.close()">

28. 最大化

<object id=big classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">

<param name="Command" value="Maximize"></object><input type=button value=最大化 onClick=big.Click()>

29. 关闭输入法

<input style="ime-mode:disabled" value=关闭输入法>

30. 链接按钮1

<input type="button" value="链接按钮1" onClick="window.open('http://www.winliuxq.com/', 'Sample', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=yes,width=790,height=520,left=0,top=0')" name="input">

链接按钮2

<input type="BUTTON" NAME="Button" value="链接按钮2" onClick="showModalDialog('http://www.winliuxq.com/')">

链接按钮3

<input type="submit" value="链接按钮3" onClick="location.href='http://www.winliuxq.com/'">

31. 警告框显示源代码

<BUTTON onClick=alert(document.documentElement.outerHTML) style="width:110">警告框显示源代码</BUTTON>

32. 点击后按钮清空

<input type=button value='打印' onClick="this.style.visibility='hidden';window.print();">

33. 打印

<input type=button value='打印' onClick="window.print();">

34. 打印预览

<OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height=0 id=wb name=wb width=0></OBJECT>

<input type=button value=打印预览 οnclick="wb.execwb(7,1)">

35. 另存为

<input onClick="document.execCommand('saveas','true','常用代码.htm')" type=button value=另存为>

36. 点击自动复制

<script>function oCopy(obj){obj.select();js=obj.createTextRange();js.execCommand("Copy")}</script>

<input type="text" value="点击自动复制" onClick="oCopy(this)" size="11">

37. 自动选中

<input value="自动选中" onFocus="this.select()" onMouseOver="this.focus()" size="11">

38. 打开源代码

<BUTTON onClick="document.location = 'view-source:' + document.location" size="7">打开源代码</BUTTON>

39. 新窗口延迟打开

<input type=button value=新窗口延迟打开 onClick=javascript:setTimeout("window.open('http://www.winliuxq.com/')",10000)>

40. 实现选中文本框里的前一半的内容

<input type="text" value="选中文本框里的前一半的内容" size=30 οnmοuseοver="this.select();tR=document.selection.createRange();tR.moveEnd('character',-8);tR.select();">

<input type="text" value="选中部分内容,非IE可以用这个" size=30 οnmοuseοver="this.selectionStart=this.value.length-4;this.selectionEnd=this.value.length">

41. 点击清空文字

<input type="text" name="artist" size=14 value="点击清空文字" οnmοuseοver=this.focus() οnfοcus=this.select() οnclick="if(this.value=='点击清空文字')this.value=''">

点击清空文字

<input name=name size=11 value=点击清空文字 onMouseOver=this.focus() οnblur="if (this.value =='') this.value='点击清空文字'" onFocus=this.select() onClick="if (this.value=='点击清空文字') this.value=''">

42. 等于标题(title):

<input type="text" value="" id="aa" size="20">

<script>document.getElementById("aa").value=document.title;</script>

43. 检测IE是否脱机

<input type="button" value="测试" οnclick="alert(window.navigator.onLine)">

44. 11种刷新按钮的方法

<input type=button value=刷新 onClick="history.go(0)">

<input type=button value=刷新 onClick="location.reload()">

<input type=button value=刷新 onClick="location=location">

<input type=button value=刷新 onClick="location.assign(location)">

<input type=button value=刷新 onClick="document.execCommand('Refresh')">

<input type=button value=刷新 onClick="window.navigate(location)">

<input type=button value=刷新 onClick="location.replace(location)">

<input type=button value=刷新 onClick="window.open('自身的文件','_self')">

<input type=button value=刷新 onClick=document.all.WebBrowser.ExecWB(22,1)>

<OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>

<form action="自身的文件"><input type=submit value=刷新></form>

<a id=a1 href="自身的文件"></a><input type=button value=刷新 onClick="a1.click()">

<a href="#" onClick=document.execCommand("open")>打开</a>

<a οnclick="window.open('i001.htm','','height=300,width=300,resizable=no,location=net');" href="">打开指定大小网页</a>

<a href="#" onClick=location.replace("view-source:"+location)>使用 记事本 编辑</a>

<a href="#" onClick=document.execCommand("saveAs")>另存为</a>

<a href="#" onClick=document.execCommand("print")>打印</a>

<a href="javascript:window.print();">打印</a>

<a href=mailto:429752806@163.com>发送E-mail</a>

<a href="#" onClick=document.execCommand("selectAll")>全选</a>

<a href="#" onClick=location.reload()>刷新1</a>

<a href="#" onClick=history.go(0)>刷新2</a>

<a href="#" onClick=location.replace("view-source:"+location)>查看源文件</a>

<a href="#" onClick=window.open(document.location,"url","fullscreen")>全屏显示</a>

<a href="#" onClick=window.external.showBrowserUI("PrivacySettings",null)>internet选项</a>

<a href="#" οncοntextmenu="window.open(this.href);return false;">单击右键将在新窗口中打开</a>

45. 后退

<a href="#" onClick=history.go(1)>前进1</a>

<a href="#" onClick=history.forward()>前进2</a>

<a href="#" onClick=history.go(-1)>后退1</a>

<a href="#" onClick=history.back()>后退2</a>

46. 收藏夹

<a href="#" onClick=window.external.showBrowserUI("OrganizeFavorites",null)>整理收藏夹</a>

<SPAN onClick="window.external.addFavorite('http://www.winliuxq.com/','网页特效站点 ')" style="CURSOR: hand" title=网页特效站点>加入收藏</SPAN>

<a href="#" onClick="window.external.addFavorite('http://www.winliuxq.com/','网页特效站点')">添加到收藏夹</A>

<a href="javascript:window.external.AddFavorite('http://www.winliuxq.com/', '网页特效站点')">点击加入收藏夹</a>

<a href="#" οnmοuseοver="window.external.addFavorite('http://www.winliuxq.com/','网页特 效站点')" target="_self" >鼠标感应收藏本站</a>

<a href="#" οnmοuseοver="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.yiloo.com/js/');" target="_self">鼠标感应设为首页</a>

47. 关闭窗体

<a href="javascript:window.close()">关闭窗口</a>

<a href="#" onClick=window.close();return false)>关闭窗口</a>

<a href="#" onClick=setTimeout(window.close(),3000)>3秒关闭本窗口</a>

<script>

function shutwin(){

window.close();

return;}

</script>

<a href="javascript:shutwin();">关闭本窗口</a>

48. 设为首页

<SPAN onClick="var strHref=window.location.href;this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.winliuxq.com/');" style="CURSOR: hand">设为首页</SPAN>

48、等于标题栏:<script>document.write(document.title);</script>

49、

<a href="javascript:void(0);" onClick='window.external.AddFavorite(location.href, document.title);'>收藏本页</a>

<a href="javascript:window.external.AddFavorite(document.location.href, document.title)">收藏本页</a>

<a href=javascript:window.external.addChannel("typhoon.cdf")>加入频道</a>

50、

<a href="i003.htm" οnclick="return false;" οndblclick="window.open('i003.htm');">双击打开链接</a>

51、

<style>#close a:hover {background:url(javascript:window.opener=0;window.close());}</style><div id=close><a href="">关闭窗口</a></div>

52、

<A HREF="javascript:void(0)" onMouseover="alert('对不起,禁止选中!')">链接禁止</A>

53、滚动条在左侧,将<html>改为<HTML DIR="RTL">

49. 网页半透明

<body style="filter:Alpha(Opacity=50)">

50. 随机选择背景色

<body>

<script>

document.body.style.background=(["red","blue","pink","navy","gray","yellow","green","purple"])[parseInt(Math.random()*8)];

</script>

51. 框架页中不显示滚动条:

<SCRIPT>

self.moveTo(0,0)

self.resizeTo(screen.availWidth,screen.availHeight)

</SCRIPT>

52. 防止网页被框架

<SCRIPT LANGUAGE=JAVASCRIPT>

if (top.location !== self.location) {

top.location=self.location;

}

</SCRIPT>

53. 永远都会带着框架

<script language="javascript"><!--

if (window == top)top.location.href = "frame.htm"; //frame.htm为框架网页

// --></script>

54. 窗口自动最大化

<script language="JavaScript"><!--

self.moveTo(0,0)

self.resizeTo(screen.availWidth,screen.availHeight)

//--></script>

55. 打开窗口自动最大化

<OBJECT classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11" onreadystatechange="if (this.readyState==4) this.Click();" VIEWASTEXT><PARAM name="Command" value="Maximize"></OBJECT>

56. 爽眼闪屏代码

<script>var color = new Array;color[1] = "black";color[2] = "white";for(x = 0; x <3; x++){document.bgColor = color[x];if(x == 2){x = 0;}}</script>

57. 不能被另存为

<noscript><iframe src=*.html></iframe></noscript>

58. 汉字字库调用

<script>

for(i=19968;i<40870;i++)document.write(String.fromCharCode(i));

</script>

59. 显示现在时间的脚本

<script language=vbscript>document.write now</script>

60. 显示最后修改时间的脚本

<script>document.write(document.lastModified)</script>

61. 按下F12键,直接返回首页

<script>function look(){

if(event.keyCode==123){document.location.href=http://www.winliuxq.com/}

}

if(document.οnkeydοwn==null)

{document.οnkeydοwn=look}

</script>

62. 端口检测

<img src="http://www.winliuxq/zza.jpg" οnlοad="alert('端口可用')" οnerrοr="alert('端口禁止')"...>

63. 无法最小化的窗口

<body οnblur='self.focus();'>

64. 链接点外部css文件

<style>@import url("ie.css");</style>

65. 内嵌式框架-网页中调用另外网页:

<object type="text/x-scriptlet" width="600"  height="1000" data="http://www.winliuxq.com/"></object>

66. 刷新改变窗口大小

<OBJECT classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11" onreadystatechange="if (this.readyState==4) this.Click();" VIEWASTEXT><PARAM name="Command" value="Maximize"></OBJECT>

67. JavaScript实现网页竖虚线

<script>hei=120;d1=2;d2=2;cou=Math.floor(hei/(d1+d2));

document.write('<table cellspacing=0 cellpadding=0 width=1 height='+hei+'>');

for(i=0;i<cou;i++){document.write('<tr><td height='+d2+'><tr><td height='+d1+' bgcolor=333333>')}</script></table>

68. js翻页

<script>document.write("<a href="+location.href.replace(/\.html/g,"_2.html")+">2</a>");</script>

转载于:https://www.cnblogs.com/naariah/articles/2105173.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
73种网页常用js代码. 比如: 1、后退 前进 <input type="button" value="后退" onClick="history.go(-1)"> <input type="button" value="前进" onClick="history.go( 1 );return true;"> 2、返回 <form><input type="button" value="返回上一步" onClick="history.back(-1)"></form> 3、查看源码 <input type="button" name="view" value="查看源码" onClick="window.location="view-source:" +window.location.href"> 4、禁止查看源码 <body oncontextmenu="return false"></body> 5、刷新按钮一 <input type="button" value="刷新按钮一" onClick="ReloadButton()"> <script>function ReloadButton(){location.href="i001.htm";}</script> 刷新按钮二 <input type="button" value="刷新按钮二" onClick="history.go(0)"> 6、回首页按钮 <input type="button" value="首页" onClick="HomeButton()"> <script>function HomeButton(){location.href=http://www.winliuxq.com;}</script> 7、弹出警告框 <input type="button" value="弹出警告框" onClick="AlertButton()"> <script>function AlertButton(){window.alert("要多多光临呀!");}</script> 8、状态栏信息 <input type="button" value="状态栏信息" onClick="StatusButton()"> <script>function StatusButton(){window.status="要多多光临呀!";}</script> 9、背景色变换 <form><input type="button" value="背景色变换" onClick="BgButton()"></form> <script>function BgButton(){ if (document.bgColor=='#00ffff') {document.bgColor='#ffffff';} else{document.bgColor='#00ffff';} } </script> 10、打开新窗口 <input type="button" value="打开新窗口" onClick="NewWindow()"> <script>function NewWindow(){window.open("c01.htm","","height=240,width=340,status=no,location=no,toolbar=no,directories=no,menubar=no");} </script> 11、窗口最小化 <OBJECT id="min" type="application/x-oleobject" classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11"><PARAM name="Command" value="Minimize"></OBJECT><button onClick="min.Click()">窗口最小化</button> 12、全屏代码 <input type="BUTTON" name="FullScreen" value="全屏显示" onClick="window.open(document.location, 'butong_net', 'fullscreen')"> 13、关闭窗口 <OBJECT id=closes type="application/x-oleobject" classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11"><param name="Command" value="Close"></object><input type="button" value="关闭窗口" onClick="closes.Click();"> 关闭窗口 <input type=button value=关闭窗口 onClick="javascript:self.close()"> 14、最大化 <object id=big classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11"> <param name="Command" value="Maximize"></object><input type=button value=最大化 onClick=big.Click()> 15、关闭输入法 <input style="ime-mode:disabled" value=关闭输入法> 。。。。。。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值