jsp;

来源:互联网 发布:淘宝上花呗怎么开通 编辑:程序博客网 时间:2024/05/22 11:26

1.格式
  <script type="text/javascript">
     document.write("");

    alert("Hello,JavaScript!");

    var greeting = "Welcome to the JS!";
    document.write(greeting);


   var result1,result2;
   result1 = (3>4)?"Y":"N";

   var today = new Date();
   var hour = today.getHours();
   document.write(today+"<br>");
   document.write(hour);
  
     function test()
   {
    document.write("a");
    document.write("b");
   }


  </script>


2.从外部引入JavaScript
 <script language="JavaScript" src="22222.js"></script>


3.数组

    <html>
 <head>
  <title>
  JavaScript数组
  </title>
  <Script Language="JavaScript">
   var arr = new Array(3);
   arr[0] = 1;
   arr[1] = 2;
   arr[2] = 3;
   arr[3] = 4;
   arr["rrr"] = 4090;
  </Script>
 </head>
 <body>
 <Script language="JavaScript">
  //document.write(arr["rrr"]);
  //document.write(arr.length);
  for(var o in arr) {
   //document.write(o);
   document.write(arr[o]);
  }
 </Script>
 </body>
</html>
4,判断表传的值是否非空

<form  name ="frm" method="get" action="ReplyOk.jsp" onsubmit="return check()">

<script language="javascript">
   //去掉头空格
   function LTrim(str) {
     var i;
     for(i=0;i<str.length;i++) {
        if(str.charAt(i)!="")break;
     }
     str = str.substring(i,str.length);
     return str;
   }
   function RTrim(str) {
     var i;
     for(i=str.length-1;i>=0;i--){
       if(str.charAt(i)!="")break;
     }
     str = str.substring(0,i+1);
     return str;
   }
   function Trim(str) {
     return LTrim(RTrim(str));
   }
   function check(){
     if(Trim(document.frm.title.value)=="") {
      alert("please input the title!");
      document.frm.title.focus();
      return false;
     }
    
     if(Trim(document.frm.cont.value)=="") {
        alert("please intput the content!");
        document.frm.cont.focus();
        return false;
     }
     return true;
   }
</script>
5.弹窗
<html>
 <head>
  <title>
  函数
  </title>
 <Script language="JavaScript">
 var userName = prompt("请输入你的名字:");
   document.write("你好!"+userName);
 </Script>
 
 </head>
 <body>
 </body>
</html>

6.删除文件

<html>
 <head>
  <title>
  函数
  </title>
 <Script language="JavaScript">
 function confirmit()
 {
   if(confirm("你确认删除此文件么?"))
   {
    document.testa.submit();
   }
  }   
 </Script> 
 </head>
 <body>
 <form name="testa" action="ttt.jsp" method="post">
  <input type="button" value="删除文件" onclick="JavaScript:confirmit()" name="aaa">
 </form>
 </body>
</html>
7.鼠标移动

<html>
 <head>
  <title>
  本地窗口
  </title>
 
 </head>
 <body onload="window.status='Hello,Please look here!'">

 
 <form name="ttt">
  <input type="button" value="test" onMouseOver="window.status='haha';">
 </form>
 </body>
</html>
8.新开窗口
<html>
 <head>
  <title>
  新开窗口
  </title>
 </head>
 <body onload="javascrip: open()">
 <Script language="JavaScript">
 //window.open("1.htm","newWin","toolbar=no,left=200,top=100,menubar=no,width=100,height=100,resizable=no");
 //window.open("1.htm","newWin","toolbar=no,left=200,top=100,menubar=no,width=100,height=100,resizable=no");
 //window.open("1.htm");
 </Script>
 <Script language="JavaScript">
   function open()
   {
       //alert('ok');
     window.open("1.htm","newWin","toolbar=no,left=200,top=100,menubar=no,width=100,height=100,resizable=no");
  }
 </Script>
 </body>
</html>
9.在打开一个窗口 另一个窗口也打开
<html>
 <head>
  <title>
  新开窗口
  </title>
 
 </head>
 <body>
 <Script language="JavaScript">
 var abc = window.open("1.htm","newWin","toolbar=no,left=200,top=100,menubar=no,width=300,height=100");
 </Script>
 <input type="button" onclick="JavaScript:abc.focus();" value="跑前面来">
 <input type="button" onclick="JavaScript:abc.close();" value="关闭">
 </body>
</html>
10.两个窗口的跳转
<html>
 <head>
  <title>
  本地窗口
  </title>
 
 </head>
 <body>
 <Script language="JavaScript">
 function go2()
 {
   window.location="33.htm";
 }
 </Script>
 <input type="button" value="转向" onclick="JavaScript:go2()">
 </body>
</html>

11.返回历史页面

<html>
 <head>
  <title>
  本地窗口
  </title>
 
 </head>
 <body>
 <Script language="JavaScript">
 function goBack()
 {
   history.back();
 }
 </Script>
 <input type="button" value="返回" onclick="JavaScript:goBack()">
 </body>
</html>
12.计算你在某个页面停留多久
<html>
 <HEAD>
 </HEAD>

<BODY>
 <form name="myform">
  您在此停留了:
  <input name="clock" size="18" value="在线时间">
 </form>

 <Script language="JavaScript">
 
 var id, iM = 0, iS = 1;
 start = new Date();

 function go()
 {
  now = new Date();
  time = (now.getTime() - start.getTime()) / 1000;
  time = Math.floor( time);
  
  //秒
  iS = time % 60;
  //分
  iM = Math.floor( time / 60);

  if ( iS < 10)
   document.myform.clock.value = " " + iM + " 分 0" + iS + " 秒";
  else
   document.myform.clock.value = " " + iM + " 分 " + iS + " 秒";
  id = setTimeout( "go()", 1000);
 }
 go();
    </Script>

</BODY>

</html>
13.检查邮箱的合法性


<HTML>

 <HEAD>
  <Script Language="JavaScript">

   function check()
   {
    var tmp,str2;
    //获得text1的值
    str2=document.myform.text1.value;
    
    tmp=str2.indexOf(
'@');
    
    if(tmp<0)
     //字符串中不包含@字符
     alert("请输入合法Email地址");
    else
     //输出 @ 的位置
     alert(tmp);
   }
   
  </Script>
 </HEAD>

 <BODY>
   <form name="myform">
  请输入Email地址:<br>
  <input type=text value="" name="text1">
  <p><input type=button value="提交" name="btn1" onClick="check()">
   </form> 
 </BODY>

</HTML>

 

 

 

13.触发事件

    1.点击 onclick=""

    2.聚焦 onFocus="JavaScript:alert(document.test.userName.value)

   3. 值改变 <input type="text" name="userName" value="SXT" onChange="JavaScript:alert(document.test.userName.value);">


   4.打开和关闭页面的时候出现
   <html>
 <head>
 </head>
 <body onLoad="javascript:alert('hello');" onUnload="javascript:alert('bye-bye');">
  nihao
 </body>
 
</html>

    5.鼠标事件onmouseover="'"
            onmouseout="''"


    6.鼠标选择时  <input type="text" name="userName" value="SXT" onSelect="JavaScript:alert(document.test.userName.value);">