白吃篇
样子
1<html>
2<head>
3<title>This is a JavaScript example</title>
4<script language="JavaScript">
5<!--
6document.write("Hello World!");
7//-->
8</script>
9</head>
10<body> Hi, man! </body>
11</html>
1<html>
2<head>
3<title>This is a JavaScript example</title>
4<script language="JavaScript">
5<!--
6document.write("Hello World!");
7//-->
8</script>
9</head>
10<body> Hi, man! </body>
11</html>
样子2
1<html>
2<head></head>
3<body>
4<script>
5..// The code embedded in the <body> tags.
6</script>
7</body>
8</html>
1<html>
2<head></head>
3<body>
4<script>
5..// The code embedded in the <body> tags.
6</script>
7</body>
8</html>
引用Js文件
1<script language="JavaScript" src="hello.js"></script>
Note:
1<script language="JavaScript" src="hello.js"></script>
- Including an external file only functions reliably across platforms in the version 4 browsers.
- The code can't include tags
<script language...>
and</script>
, or you will get an error message.
Write and Writeln
1<PRE nd="27"><HTML>
2<HEAD>
3<TITLE> Welcome to my site</TITLE></HEAD>
4<BODY>
5<SCRIPT LANGUAGE="JAVASCRIPT">
6<!--
7document.write("Welcome to my site!");
8// -->
9</SCRIPT>
10</BODY>
11</HTML></PRE>
1<PRE nd="27"><HTML>
2<HEAD>
3<TITLE> Welcome to my site</TITLE></HEAD>
4<BODY>
5<SCRIPT LANGUAGE="JAVASCRIPT">
6<!--
7document.write("Welcome to my site!");
8// -->
9</SCRIPT>
10</BODY>
11</HTML></PRE>
Document object
1document.write("Hi there.")
1document.write("Hi there.")
lastModified
1<script language="JavaScript">
2document.write("This page created by John N. Last update:" + document.lastModified);
3</script>
1<script language="JavaScript">
2document.write("This page created by John N. Last update:" + document.lastModified);
3</script>
bgColor and fgColor
1<script>
2document.bgColor="black"
3document.fgColor="#336699"
4</script>
1<script>
2document.bgColor="black"
3document.fgColor="#336699"
4</script>
Message Box
1<body>
2<script>
3window.alert("Welcome to my site!")
4</script>
5</body>
1<body>
2<script>
3window.alert("Welcome to my site!")
4</script>
5</body>
confirm
1window.confirm("Are you sure you want to quit?")
1window.confirm("Are you sure you want to quit?")
prompt
1window.prompt("please enter user name")
1window.prompt("please enter user name")
Variables and Conditions
1<script>
2var x=window.confirm("Are you sure you want to quit")
3
4if (x)
5 window.alert("Thank you.")
6else
7 window.alert("Good choice.")
8</script>
1<script>
2var x=window.confirm("Are you sure you want to quit")
3
4if (x)
5 window.alert("Thank you.")
6else
7 window.alert("Good choice.")
8</script>
prompt
1<script>
2var y=window.prompt("please enter your name")
3window.alert(y)
4</script>
1<script>
2var y=window.prompt("please enter your name")
3window.alert(y)
4</script>
Example
1<html>
2<head>
3<script>
4var x=confirm("Are you sure you want to quit?")
5if (!x)
6 window.location="http://www.yahoo.com"
7</script>
8</head>
9<body>
10Welcome to my website!.
11</body></html>
1<html>
2<head>
3<script>
4var x=confirm("Are you sure you want to quit?")
5if (!x)
6 window.location="http://www.yahoo.com"
7</script>
8</head>
9<body>
10Welcome to my website!.
11</body></html>
Function
1function test()
2{
3 document.write("Hello can you see me?")
4}
1function test()
2{
3 document.write("Hello can you see me?")
4}
调用事件
1function test()
2{
3 document.write("Hello can you see me?")
4}
5test()
1function test()
2{
3 document.write("Hello can you see me?")
4}
5test()
Event handler
1<script>
2function ss()
3{
4alert("Thank you!")
5}
6</script>
7<form>
8<input type="button" value="Click here" onclick="ss()">
9</form>
1<script>
2function ss()
3{
4alert("Thank you!")
5}
6</script>
7<form>
8<input type="button" value="Click here" onclick="ss()">
9</form>
onLoad
1<body onload="ss()">
2<frameset onload="ss()">
3<img src="whatever.gif" onload="ss()">
1<body onload="ss()">
2<frameset onload="ss()">
3<img src="whatever.gif" onload="ss()">
onMouseover,onMouseout
1<a href="#" onMouseOver="document.write('Hi, nice to see you!">Over Here!</a>
2<a href="#" onMouseOut="alert('Good try!')">Get Out Here!</a>
1<a href="#" onMouseOver="document.write('Hi, nice to see you!">Over Here!</a>
2<a href="#" onMouseOut="alert('Good try!')">Get Out Here!</a>
onUnload
1<body onunload="alert('Thank you for visiting us. See you soon')">
1<body onunload="alert('Thank you for visiting us. See you soon')">
Handle multiple actions
1<form>
2<input type="button" value="Click here!" onClick="alert('Thanks
3for visiting my site!');window.location='http://www.yahoo.com'">
4</form>
1<form>
2<input type="button" value="Click here!" onClick="alert('Thanks
3for visiting my site!');window.location='http://www.yahoo.com'">
4</form>
Form
1<form name="aa">
2<input type="text" size="10" value="" name="bb"><br>
3<input type="button"
4value="Click Here"onclick="alert(document.aa.bb.value)">
5</form>
1<form name="aa">
2<input type="text" size="10" value="" name="bb"><br>
3<input type="button"
4value="Click Here"onclick="alert(document.aa.bb.value)">
5</form>
onBlur
1<html><head><script>
2function emailchk()
3{
4var x=document.feedback.email.value
5if (x.indexOf("@")==-1)
6{
7 alert("It seems you entered an invalid email address.")
8 document.feedback.email.focus()
9}
10}
11</script></head><body>
1<html><head><script>
2function emailchk()
3{
4var x=document.feedback.email.value
5if (x.indexOf("@")==-1)
6{
7 alert("It seems you entered an invalid email address.")
8 document.feedback.email.focus()
9}
10}
11</script></head><body>
调用OnBlur
1<form
2name="feedback">
3Email:<input type="text" size="20" name="email"
4onblur="emailchk()"><br>
5Comment: <textarea name="comment" rows="2" cols="20"></textarea><br>
6<input type="submit" value="Submit">
7</form>
8</body></html>
1<form
2name="feedback">
3Email:<input type="text" size="20" name="email"
4onblur="emailchk()"><br>
5Comment: <textarea name="comment" rows="2" cols="20"></textarea><br>
6<input type="submit" value="Submit">
7</form>
8</body></html>
Example
1<script>
2<!--
3function validate()
4{
5if(document.login.userName.value=="")
6{
7 alert ("Please enter User Name")
8 return false
9}
10if(document.login.password.value=="")
11{
12 alert ("Please enter Password")
13 return false
14}
15}
16//-->
17</script>
18<form name="login" onsubmit="return validate()">
19<input type="text" size="20" name="userName">
20<input type="text" size="20" name="password">
21<input type="submit" name="submit" value="Submit">
22</form>
1<script>
2<!--
3function validate()
4{
5if(document.login.userName.value=="")
6{
7 alert ("Please enter User Name")
8 return false
9}
10if(document.login.password.value=="")
11{
12 alert ("Please enter Password")
13 return false
14}
15}
16//-->
17</script>
18<form name="login" onsubmit="return validate()">
19<input type="text" size="20" name="userName">
20<input type="text" size="20" name="password">
21<input type="submit" name="submit" value="Submit">
22</form>
Protect a file by using Login
1<html><head>
2<SCRIPT Language="JavaScript">
3function checkLogin(x)
4{
5if ((x.id.value != "Sam")||(x.pass.value !="Sam123"))
6{
7 alert("Invalid Login");
8 return false;
9}
10else
11 location="main.htm"
12}
13</script>
1<html><head>
2<SCRIPT Language="JavaScript">
3function checkLogin(x)
4{
5if ((x.id.value != "Sam")||(x.pass.value !="Sam123"))
6{
7 alert("Invalid Login");
8 return false;
9}
10else
11 location="main.htm"
12}
13</script>
Protect a file by using Login
1</head><body>
2<form>
3<p>UserID:<input type="text" name="id"></p>
4<p>Password:<input type="password" name="pass"></p>
5<p><input type="button" value="Login" onClick="checkLogin(this.form)"></p>
6</form>
7</body></html>
1</head><body>
2<form>
3<p>UserID:<input type="text" name="id"></p>
4<p>Password:<input type="password" name="pass"></p>
5<p><input type="button" value="Login" onClick="checkLogin(this.form)"></p>
6</form>
7</body></html>
Link
1<a href="JavaScript:window.location.reload()">Click to reload!</a>
2<a href="#" onClick="alert('Hello, world!')">Click me to say Hello</a><br>
3<a href="#" onMouseOver="location='main.htm'">Mouse over to see Main Page</a>
1<a href="JavaScript:window.location.reload()">Click to reload!</a>
2<a href="#" onClick="alert('Hello, world!')">Click me to say Hello</a><br>
3<a href="#" onMouseOver="location='main.htm'">Mouse over to see Main Page</a>
Date
1<HTML><HEAD><TITLE>Show
2Date</TITLE></HEAD>
3<BODY>
4<SCRIPT LANGUAGE="JavaScript">
5var x= new Date();
6document.write (x);
7</SCRIPT>
8</BODY></HTML>
1<HTML><HEAD><TITLE>Show
2Date</TITLE></HEAD>
3<BODY>
4<SCRIPT LANGUAGE="JavaScript">
5var x= new Date();
6document.write (x);
7</SCRIPT>
8</BODY></HTML>
Dynamically display different pages
1var banTime= new Date()
2var ss=banTime.getHours()
3if (ss<=12)
4 document.write("<img src='banner1.gif'>")
5else
6 document.write("<img src='banner2.gif'>")
1var banTime= new Date()
2var ss=banTime.getHours()
3if (ss<=12)
4 document.write("<img src='banner1.gif'>")
5else
6 document.write("<img src='banner2.gif'>")
Date object Methods
1getDate
2getTime
3getTimezoneOffset
4getDay
5getMonth
6getYear getSeconds
7getMinutes
8getHours
9
1getDate
2getTime
3getTimezoneOffset
4getDay
5getMonth
6getYear getSeconds
7getMinutes
8getHours
9
Open a window
1<form>
2<input type="button" value="Click here to see" onclick="window.open('test.htm')">
3</form>
1<form>
2<input type="button" value="Click here to see" onclick="window.open('test.htm')">
3</form>
Size, toolbar, menubar, scrollbars, location, status
1open("URL","name","attributes")
2For example:
3<form>
4<input type="button" value="Click here to see"
5onclick="window.open('page2.htm','win1','width=200,height=200,menubar')">
6</form>
7Another example with no attributes turned on, except the size changed:
8<form>
9<input type="button" value="Click here to see"
10onclick="window.open('page2.htm','win1','width=200,height=200')">
11</form>
1open("URL","name","attributes")
2For example:
3<form>
4<input type="button" value="Click here to see"
5onclick="window.open('page2.htm','win1','width=200,height=200,menubar')">
6</form>
7Another example with no attributes turned on, except the size changed:
8<form>
9<input type="button" value="Click here to see"
10onclick="window.open('page2.htm','win1','width=200,height=200')">
11</form>
Here is the complete list of attributes you can add:
1width height toolbar
2location directories status
3scrollbars resizable menubar
4
1width height toolbar
2location directories status
3scrollbars resizable menubar
4
Reload
1window.location.reload()
1window.location.reload()
Close Window
1<form>
2<input type="button" value="Close Window" onClick="window.close()">
3</form>
4<a href="javascript:window.close()">Close Window</a>
1<form>
2<input type="button" value="Close Window" onClick="window.close()">
3</form>
4<a href="javascript:window.close()">Close Window</a>
Loading
1window.location="test.htm"
2
3<a href="test.htm>Try this </a>
4
5<script>
6<!--
7function ss()
8{
9var ok=confirm('Click "OK" to go to yahoo, "CANCEL" to go to hotmail')
10if (ok)
11location="http://www.yahoo.com"
12else
13location="http://www.hotmail.com"
14}
15//-->
16</script>
17
18
1window.location="test.htm"
2
3<a href="test.htm>Try this </a>
4
5<script>
6<!--
7function ss()
8{
9var ok=confirm('Click "OK" to go to yahoo, "CANCEL" to go to hotmail')
10if (ok)
11location="http://www.yahoo.com"
12else
13location="http://www.hotmail.com"
14}
15//-->
16</script>
17
18
Remote Control Window
1Let's say you have opened a new window from the current window. After that, you will wonder how to make a control between the two windows. To do this, we need to first give a name to the window.Look at below:
2
3aa=window.open('test.htm','','width=200,height=200')
4
5By giving this window a name "aa", it will give you access to anything that's inside this window from other windows. Whenever we want to access anything that's inside this newly opened window, for example, to write to this window, we would do this: aa.document.write("This is a test.").
6
7example:
8<html><head><title></title></head>
9<body>
10<form>
11<input type="button" value="Open another page"
12onClick="aa=window.open('test.htm','','width=200,height=200')">
13<input type="radio" name="x" onClick="aa.document.bgColor='red'">
14<input type="radio" name="x" onClick="aa.document.bgColor='green'">
15<input type="radio" name="x" onClick="aa.document.bgColor='yellow'">
16</form>
17</body></html>
1Let's say you have opened a new window from the current window. After that, you will wonder how to make a control between the two windows. To do this, we need to first give a name to the window.Look at below:
2
3aa=window.open('test.htm','','width=200,height=200')
4
5By giving this window a name "aa", it will give you access to anything that's inside this window from other windows. Whenever we want to access anything that's inside this newly opened window, for example, to write to this window, we would do this: aa.document.write("This is a test.").
6
7example:
8<html><head><title></title></head>
9<body>
10<form>
11<input type="button" value="Open another page"
12onClick="aa=window.open('test.htm','','width=200,height=200')">
13<input type="radio" name="x" onClick="aa.document.bgColor='red'">
14<input type="radio" name="x" onClick="aa.document.bgColor='green'">
15<input type="radio" name="x" onClick="aa.document.bgColor='yellow'">
16</form>
17</body></html>
opener
1Using "opener" property, we can access the main window from the newly opened window.
2
3<html>
4<head>
5<title></title>
6</head>
7<body>
8<form>
9<input type="button" value="Open another page"
10onClick="aa=window.open('test.htm','','width=100,height=200')">
11</form>
12</body>
13</html>
14
15<html>
16<head>
17<title></title>
18<script>
19function remote(url){
20window.opener.location=url
21}
22</script>
23</head>
24<body>
25<p><a href="#" onClick="remote('file1.htm')">File
261</a></p>
27<p><a href="#" onClick="remote('file2.htm')">File
282</a></p>
29</body>
30</html>
1Using "opener" property, we can access the main window from the newly opened window.
2
3<html>
4<head>
5<title></title>
6</head>
7<body>
8<form>
9<input type="button" value="Open another page"
10onClick="aa=window.open('test.htm','','width=100,height=200')">
11</form>
12</body>
13</html>
14
15<html>
16<head>
17<title></title>
18<script>
19function remote(url){
20window.opener.location=url
21}
22</script>
23</head>
24<body>
25<p><a href="#" onClick="remote('file1.htm')">File
261</a></p>
27<p><a href="#" onClick="remote('file2.htm')">File
282</a></p>
29</body>
30</html>
Frame
1<html>
2<frameset cols="150,*">
3<frame src="page1.htm" name="frame1">
4<frame src="page2.htm" name="frame2">
5</frameset>
6</html>
7
8<html>
9<body>
10<h2>This is page 1 </h2>
11<a href="page3.htm"
12onClick="parent.frame2.location='page4.htm'">Click Here</a>
13</body>
14</html>
Notice: You should use 1<html>
2<frameset cols="150,*">
3<frame src="page1.htm" name="frame1">
4<frame src="page2.htm" name="frame2">
5</frameset>
6</html>
7
8<html>
9<body>
10<h2>This is page 1 </h2>
11<a href="page3.htm"
12onClick="parent.frame2.location='page4.htm'">Click Here</a>
13</body>
14</html>
"parent.frameName.location"
to access another frame. "parent" standards for the parent frame containing the frameset code.