一下是csschina.net 收集整理的一些常用的JS代码 ,希望能提高各位Web开发的效率。
Ctrl+Enter发表帖子
以下是代码:
- <body onkeydown="if(event.keyCode==13 && event.ctrlKey)messager.submit()">
- <form action="1.htm" method=post name=messager>按Ctrl+Enter试试</form>
- </body>
浏览者自己控制字体大小
以下是代码:
- 【<A href="javascript:doZoom(16)">大</A> <A href="javascript:doZoom(14)">中</A> <A href="javascript:doZoom(12)">小</A>】
- <SCRIPT language=javascript>
- function doZoom(size){
- document.getElementById('zoom').style.fontSize=size+'px'
- }
- </script>
- <br><br>
- <div id=zoom>测试</div>
检测文本框中字符串的长度,汉字算2个,英文算1个
以下是代码:
- <script>
- function checkstr(str)
- {
- num=str.length
- var arr=str.match(/[^\\\\\\\\\\\\\\\\x00-\\\\\\\\\\\\\\\\x80]/ig)
- if(arr!=null)num+=arr.length
- alert(num)
- }
- </script>
- <input id="msg" value="在线goaler.xicp.net"><input type=button onclick=checkstr(msg.value) value="检查">
不刷新的写屏
以下是代码:
- <html>
- <head>
- <title></title>
- <script language="javascript">
- step=0;
- function flash_title(){
- step++;
- if (step==3) {step=1};
- if (step==1) {document.title='☆☆☆☆☆☆☆☆☆☆'};
- if (step==2) {document.title='★★★★★★★★★★'};
- setTimeout("flash_title()",600);};
- flash_title()
- </script>
- </head>
- </html>
取得键盘的KeyCode
以下是代码:
- <html>
- <!--
- <head>
- <script language="javascript">
- ns4 = (document.layers) ? true : false;
- ie4 = (document.all) ? true : false;
- function keyDown(e){
- if(ns4){
- var nkey=e.which;
- var iekey='现在是ns浏览器';
- var realkey=String.fromCharCode(e.which);
- }
- if(ie4){
- var iekey=event.keyCode;
- var nkey='现在是ie浏览器';
- var realkey=String.fromCharCode(event.keyCode);
- if(event.keyCode==32){realkey='\' 空格\''}
- if(event.keyCode==13){realkey='\' 回车\''}
- if(event.keyCode==27){realkey='\' Esc\''}
- if(event.keyCode==16){realkey='\' Shift\''}
- if(event.keyCode==17){realkey='\' Ctrl\''}
- if(event.keyCode==18){realkey='\' Alt\''}
- }
- alert('ns浏览器中键值:'+nkey+'\n'+'ie浏览器中键值:'+iekey+'\n'+'实际键为'+realkey);
- }
- document.onkeydown = keyDown;
- if(ns4){
- document.captureEvents(Event.KEYDOWN);}
- </script>
- </head>
- <body>
-
- <hr>
- <center>
- <h3>请按任意一个键。。。。</h3>
- </center>
- </body>
- </html>
得到当前网页名字的两种方法
以下是代码:
- <script>
- var str=location.pathname;
- a=str.substr(str.lastIndexOf("/")+1)
- alert(a)
- a=location.pathname.replace(/(.+)[\\/]/,"")
- alert(a)
- </script>
节日倒计时
以下是代码:
- <Script Language="javascript">
- var timedate= new Date("October 1,2008");
- var times="国庆节";
- var now = new Date();
- var date = timedate.getTime() - now.getTime();
- var time = Math.floor(date / (1000 * 60 * 60 * 24));
- if (time >= 0)
- document.write("现在离"+times+"还有: "+time +"天")
- </Script>
屏蔽主窗口滚动条
- 在body标签里加上 style=overflow-y:hidden
屏蔽网站的打印功能
- <style>
- @media print {
- * { display: none }
- }
- </style>
屏蔽F5键
- <script language=javascript>
- <!--
- function document.onkeydown()
- {
- if ( event.keyCode==116)
- {
- event.keyCode = 0;
- event.cancelBubble = true;
- return false;
- }
- }
- -->
- </script>
使用全屏打开页面
- <script language=javascript>
- <!--
- window.open(www.32pic.com,32pic,fullscreen=3,height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no);
- -->
- </script>
- 注:在body标签里加上onbeforeunload=javascript :return false(使不能关闭窗口)
位于页面两侧的广告[新浪网的那种]
- <Script language="javascript">
- var rightbar;
- var coupletcode;
- rightbar = window.screen.width-140;
- coupletcode = "<DIV id=sidebar style='LEFT: 0px; POSITION: absolute; TOP: 100px; VISIBILITY: hidden;' width=117>32</div><DIV id=sidebar2 style='LEFT: " + rightbar + "px; POSITION: absolute; TOP: 100px; VISIBILITY: hidden' width=117>32</div>";
- if (window.screen.width>=1024)
- {
- document.write(coupletcode);
- }
- function InsertCouplet()
- {
- if (window.screen.width>=1024){
- document.all.sidebar.style.visibility = "visible";
- document.all.sidebar2.style.visibility = "visible";
- document.all.sidebar.innerHTML = "<OBJECT classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0 WIDTH=117 HEIGHT=270 id=leftFF ALIGN=> <PARAM NAME=movie VALUE=http://images.sohu.com/cs/button/youpai/120270812b.swf?clickthru=http://goto.sohu.com/goto.php3?code=youpai-sh47-frduilian> <PARAM NAME=quality VALUE=high><PARAM NAME=wmode VALUE=transparent> <EMBED id=leftFFG src=http://images.sohu.com/cs/button/youpai/120270812b.swf?clickthru=http://goto.sohu.com/goto.php3?code=youpai-sh47-frduilian quality=high wmode=transparent WIDTH=117 HEIGHT=270 NAME=you ALIGN= TYPE=application/x-shockwave-flash PLUGINSPAGE=http://www.macromedia.com/go/getflashplayer></EMBED></OBJECT>";
- document.all.sidebar2.innerHTML = "<OBJECT classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0 WIDTH=117 HEIGHT=270 id=youFF ALIGN=> <PARAM NAME=movie VALUE=http://images.sohu.com/cs/button/youpai/120270812b.swf?clickthru=http://goto.sohu.com/goto.php3?code=youpai-sh47-frduilian> <PARAM NAME=quality VALUE=high> <PARAM NAME=wmode VALUE=transparent><EMBED id=youFFG src=http://images.sohu.com/cs/button/youpai/120270812b.swf?clickthru=http://goto.sohu.com/goto.php3?code=youpai-sh47-frduilian quality=high wmode=transparent WIDTH=117 HEIGHT=270 NAME=you ALIGN= TYPE=application/x-shockwave-flash PLUGINSPAGE=http://www.macromedia.com/go/getflashplayer></EMBED></OBJECT>";
- }
- }
- InsertCouplet();
- function leftFFG_DoFSCommand(command,args){
- if ((command == "SohuCLFS")&&(args =="hidecouplet")){
- document.all.sidebar.style.visibility = "hidden";
- document.all.sidebar2.style.visibility = "hidden";
- }
- }
- function youFFG_DoFSCommand(command,args){
- leftFFG_DoFSCommand(command,args);
- }
- <!--
- if (document.all){
- document.write("<Script language=\"javascript\" For=\"youFF\" Event=\"FSCommand(command,args)\">\n");
- document.write(" leftFFG_DoFSCommand(command,args);\n");
- document.write("</Script>\n");
- document.write("<Script language=\"javascript\" For=\"leftFF\" Event=\"FSCommand(command,args)\">\n");
- document.write(" leftFFG_DoFSCommand(command,args);\n");
- document.write("</Script>\n");
- }
-
- </script>
|