注:我对原文进行了编辑,对一些词汇标注颜色,方便阅读。本来准备翻译,但是觉得文章简单易懂,而且原文写得很好,所以就不献丑了。希望对JavaScript初学者能有所帮助。你可以跟着作者一起做那些示例代码,等读完文章的时候,你就可以掌握JavaScript的基本操作了,你会发现其实这一切很容易。
Contents
Embedding and including
write and writeln
Document object
Message box
Function
Event handler
Form
Link
Date
Window
Frame
[h1][url=http://www.yahoo.com/]http://www.yahoo.com[/url]
[h3]
Size, toolbar, menubar, scrollbars, location, status [/h3]
Let's add some of attributes to the above script to control the size of the window, and show: toolbar, scrollbars etc. The syntax to add attributes is:
open("URL","name","attributes")
For example:
< form >
< input type ="button" value ="Click here to see"
onclick ="window.open('page2.htm','win1','width=200,height=200,menubar')" >
</ form >
Another example with no attributes turned on, except the size changed:
< form >
< input type ="button" value ="Click here to see"
onclick ="window.open('page2.htm','win1','width=200,height=200')" >
</ form >
Here is the complete list of attributes you can add:
| width |
height |
toolbar |
| location |
directories |
status |
| scrollbars |
resizable |
menubar |
[h3]
Reload [/h3]
To reload a window, use this method:
window.location.reload()
[h3]
Close Window [/h3]
Your can use one of the codes shown below:
< form >
< input type ="button" value ="Close Window" onClick ="window.close()" >
</ form >
< a href ="javascript:window.close()" > Close Window </ a >
[h3]
Loading [/h3]
The basic syntax when loading new content into a window is:
window.location="test.htm"
This is the same as
< a href ="test.htm>Try this </a>
Let's provide an example, where a confirm box will allow users to choose between going to two places:
< script >
<!--
function ss()
{
var ok = confirm('Click " OK " to go to yahoo, " CANCEL " to go to hotmail')
if (ok)
location = " http://www.yahoo.com "
else
location = " http://www.hotmail.com "
}
// -->
</ script >
[h3]
Remote Control Window [/h3]
Let'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:
aa=window.open('test.htm','','width=200,height=200')
By 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.").
Now, let's see an example of how to change the background color of another window:
< html >< head >< title ></ title ></ head >
< body >
< form >
< input type ="button" value ="Open another page"
onClick ="aa=window.open('test.htm','','width=200,height=200')" >
< input type ="radio" name ="x" onClick ="aa.document.bgColor='red'" >
< input type ="radio" name ="x" onClick ="aa.document.bgColor='green'" >
< input type ="radio" name ="x" onClick ="aa.document.bgColor='yellow'" >
</ form >
</ body ></ html >
[h3]
opener [/h3]
Using "opener" property, we can access the main window from the newly opened window.
Let's create Main page:
< html >
< head >
< title ></ title >
</ head >
< body >
< form >
< input type ="button" value ="Open another page"
onClick ="aa=window.open('test.htm','','width=100,height=200')" >
</ form >
</ body >
</ html >
Then create Remote control page (in this example, that is test.htm):
< html >
< head >
< title ></ title >
< script >
function remote(url){
window.opener.location = url
}
</ script >
</ head >
< body >
< p >< a href ="#" onClick ="remote('file1.htm')" > File1 </ a ></ p >
< p >< a href ="#" onClick ="remote('file2.htm')" > File2 </ a ></ p >
</ body >
</ html >
Try it now!
Frame [/h1]
One of the most popular uses of loading multiple frames is to load and change the content of more than one frame at once. Lets say we have a parent frame:
< html >
< frameset cols ="150,*" >
< frame src ="page1.htm" name ="frame1" >
< frame src ="page2.htm" name ="frame2" >
</ frameset >
</ html >
We can add a link in the child frame "frame1" that will change the contents of not only page1, but page2 too. Shown below is the html code for it:
< html >
< body >
< h2 > This is page 1 </ h2 >
< a href ="page3.htm"
onClick ="parent.frame2.location='page4.htm'" > Click Here </ a >
</ body >
</ html >
Notice: You should use "parent.frame2.location" to access another frame. "parent" standards for the parent frame containing the frameset code.
Source:http://www.codeproject.com/jscript/jsbeginner.asp