Little and Mickle

All things are difficult before they are easy.

导航

Javascript常用脚本

1.自动选定文本框中的文本
<input type="text" name="mtext" size="100" width=20 value="
待选中的文本,把鼠标移上去看看:)" onmouseover="focus();select();">

2.输入框限制
<input type="text" name="txtMonth" value="" 
    onBlur="if(this.value<1||this.value>12)
    {alert('
月份输入有误!');
    this.focus();}>


3.
限制输入框只能输入数字金额
<input type=text name=txtPostalCode onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnvalue = false;">

4.回车转下一行
<input onkeydown="if(event.keyCode==13)event.keyCode=9" name="text1">

5.弹出普通窗口及全屏展开
<script language="javascript" type="text/javascript">
var winH=window.screen.height -58;
var winW=window.screen.width -10;
</script>
<A href='javascript:;' onclick="window.open('search_grmx.asp?sid=<%=rs("sid")%>&rq_year=<%=year(rs("rq"))%>&rq_month=<%=month(rs("rq"))%>','newwindow','scrollbars=yes,top=1,left=1,width='+winW+',height='+winH+'')">
链接文字
</a>
6.JS
验证
<SCRIPT language=javascript>
<!--
function checkform()
{
  if (document.form1.skr.value=="") {
    alert("
必须输入");
 form1.skr.focus(); 
 return false;   
  }
  return true;
}
//-->
</SCRIPT>

<form name="form1" method="post" action="enter.asp" onsubmit="return checkform();">

7.全选删除
<script language="javascript">
<!-- Begin
 var checkflag = "false";
 function check(field) {
  if (checkflag == "false") {
   for (i = 0; i < field.length; i++){
    if (field[i].disabled == false){
     field[i].checked = true;
    }
   }
   checkflag = "true";
   return "
取消全选"; 
  }
  else {
   for (i = 0; i < field.length; i++){
    field[i].checked = false; 
   }
   checkflag = "false";
   return "
全 选
"; 
  }
 }
//  End -->
</script>

<input type="checkbox" name="list" value="<%=PIC_ID%>">

<input name="button" type=button onClick="this.value=check(this.form.list)" class="bottom" value="全 选">
<input name="Submit" type=submit  class="bottom" value="
删 除">

8. text聚焦
<body text="#000000" bgcolor="#EFF1FD" onload="document.form1.txm.focus();" scroll="no">

9.限制只能输入(0~9,a~z)
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
    <SCRIPT language=javascript>
<!--
function ischinese(){
var reg=/[^a-z0-9_]/g
if (reg.test(document.form1.text3.value)){
    alert("
请输入0-9和小写的a-z");
 form1.text3.focus(); 
return false; 
}else{
return true;}
}
//-->
</SCRIPT>

</head>

<body>
<FORM name="form1" method="post" action="abc.asp" onsubmit="return ischinese();">
<input name="text3" type="text" size="15" value="">
  <input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>

(一)、设为首页
 
<a href=http://www.thwd.net onMouseOver=this.style.behavior="url(#default#homepage)";
this.setHomePage("http://www.thwd.net/";);>
设为首页</a>
(二)、收藏本站

<a href=javascript:window.external.addFavorite("http://www.jzzy.com";,"建站资源网")>收藏本站</a>


(三)、在窗口的状态区显示滚动信息
(1)
BODY中加入代码
<script language="javascript">
var msg="
欢迎访问建站资源网,在这里有你会有所收获的!";
var i=1
function scroll()
{
mess=msg.substring(i,msg.length)+" "+msg.substring(0,i)
window.status=mess
i++;
if (i>=msg.length) i=1; //
设置不停滚动
setTimeout("scroll()",200); //
设置滚动速度
}
</script>
(2)
BODY标签中:
<body onload="scroll()">
(四)、在页面加入当前时间

<script language="javascript">
tdy=new Date();
document.write("
当前时间:",tdy.getHours());
document.write(":",tdy.getMinutes());
document.write(":",tdy.getSeconds());
</script>

(五)、前进、后退按钮
<input type="button" value="<-" onclick="history.go(-1)">
<input type="button" value="->" onclick="history.go(1)">


(六)、加入页面最后修改日期
<script language="javascript">
document.write("
本页最后编辑日期:");
document.write(document.lastModified)
</script>


(七)、前进、后退按钮
<font onclick="history.go(-1)">
前一页</font>
<font onclick="history.go(-2)">
前两页</font>
<font onclick="history.go(-3)">
前三页</font>
<font onclick="history.go(1)">
后一页</font>
<font onclick="history.go(2)">
后两页</font>
<font onclick="history.go(3)">
后三页</font>
也可设置退后、前进多步

(八)、鼠标事件
<A HREF="MAILTO:webmaster@jzzy.com" onmouseover="alert("
给我写信"); return true">信箱</A>
鼠标放上去显示警告信息


(九)、获得浏览器的属性
navigator.appCodename=undefinednavigator.appName=Microsoft Internet Explorernavigator.appVersion=4.0 (compatible; MSIE 5.0; Windows 98; DigExt)navigator.appAgent=undefined
(十)、打印整个页面
<font onClick="javascript:window.print()">
打印本页</font>
(十一)、查看源码
<input TYPE="button" NAME="view" value="
查看本页的源码" onClick="window.location="view-source:" +window.location.href" class="pt9">
(十二)、刷新页面
<font onClick="history.go(0)">
刷新本页</font>
(十三)、背景色变换
<input TYPE="button" value="
背景色变换" onClick="BgButton()">
<script>function BgButton()
{
if (document.bgColor=='#00ffff')
{
document.bgColor='#ffffff';
}
else{document.bgColor='#00ffff';
}
}
</script>
(十四)、Title上显示信息
<script language="javascript1.2">
<!--hide
document.title="
今天是星期天"
//--hide-->
</script>


限制输入框只能输入数字金额
<input type=text name=txtPostalCode onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnvalue = false;">

1.  oncontextmenu="window.event.returnvalue=false"      将彻
底屏蔽鼠标右键
    <table border oncontextmenu=return(false)><td>no</table>
 
可用于Table
2.  <body onselectstart="return false">                
取消
选取、防止复制
3.  onpaste="return false"                                  
    
不准粘贴
4.  oncopy="return false;" oncut="return false;"            
    
防止复制
5.  <link rel="Shortcut Icon" href="favicon.ico"> IE
地址栏前
换成自己的图标
6.  <link rel="Bookmark" href="favicon.ico"> 
可以在收藏夹中
显示出你的图标
7.  <input style="ime-mode:disabled">                       
  
关闭输入法
8. 
永远都会带着框架
<script language="javascript"><!--
  if (window == top)top.location.href = "frames.htm";
//frames.htm
为框架网页
// --></script>
9. 
防止被人frame
<SCRIPT LANGUAGE=javascript><!--
  if (top.location !=
self.location)top.location=self.location;
// --></SCRIPT>
10. <noscript><iframe src=*.html></iframe></noscript>    

页将不能被另存为

11. <input type=button value=
查看网页源代码
 onclick="window.location = 'view-source:'+
' http://www.csdn.net/&#39;">

12.
怎样通过asp的手段来检查来访者是否用了代理
<% if Request.ServerVariables("HTTP_X_FORWARDED_FOR")<>""
then
 response.write "<font color=#FF0000>
您通过了代理服务器,"&
_
 "
真实的IP
"&Request.ServerVariables("HTTP_X_FORWARDED_FOR")
 end if
%>
13.
取得控件的绝对位置
//javascript
<script language="javascript">
function getIE(e){
  var t=e.offsetTop;
  var l=e.offsetLeft;
  while(e=e.offsetParent){
    t+=e.offsetTop;
    l+=e.offsetLeft;
    }
  alert("top="+t+"\nleft="+l);
  }
</script>
//VBScript
<script language="VBScript"><!--
function getIE()
  dim t,l,a,b
  set a=document.all.img1
  t=document.all.img1.offsetTop
  l=document.all.img1.offsetLeft
  while a.tagName<>"BODY"
    set a = a.offsetParent
    t=t+a.offsetTop
    l=l+a.offsetLeft
  wend
  msgbox "top="&t&chr(13)&"left="&l,64,"
得到控件的位置"
end function
--></script>

14.
游标是停在文本框文字的最后
<script language="javascript">
function cc()
{
  var e = event.srcElement;
  var r =e.createTextRange();
  r.moveStart('character',e.value.length);
  r.collapse(true);
  r.select();
}
</script>
<input type=text name=text1 value="123" onfocus="cc()">

15.
判断上一页的来源
asp:
request.servervariables("HTTP_REFERER")
javascript:
document.referrer
16.
最小化、最大化、关闭窗口
<object id=hh1
classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">
<param name="Command" value="Minimize"></object>
<object id=hh2
classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">
<param name="Command" value="Maximize"></object>
<OBJECT id=hh3
classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">
<PARAM NAME="Command" value="Close"></OBJECT>
<input type=button value=
最小化 onclick=hh1.Click()>
<input type=button value=
最大化 onclick=hh2.Click()>
<input type=button value=
关闭 onclick=hh3.Click()>
本例适用于IE

posted on 2005-07-29 17:38  davidullua  阅读(650)  评论(0)    收藏  举报