Rumah > hujung hadapan web > tutorial js > 国外的为初学者写的JavaScript教程_基础知识

国外的为初学者写的JavaScript教程_基础知识

WBOY
Lepaskan: 2016-05-16 19:03:54
asal
1649 orang telah melayarinya

Embedding and including

Let's first see a simple example:

<span class="code-keyword"><span class="code-leadattribute">html</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">head</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">title</span><span class="code-keyword">></span>This is a JavaScript example<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">title</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">script</span> <span class="code-attribute">language</span><span class="code-keyword">="</span><span class="code-keyword">JavaScript"</span><span class="code-keyword">></span> 
<!-- 
document.write(<SPAN class=code-string>"</SPAN><SPAN class=code-string>Hello World!"</SPAN>); 
<SPAN class=code-comment>//</SPAN><SPAN class=code-comment>--> 
</span> 
<span class="code-keyword"></span><span class="code-leadattribute">script</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">head</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">body</span><span class="code-keyword">></span> Hi, man! <span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">body</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">html</span><span class="code-keyword">></span></span></span></span></span></span></span></span></span>
Salin selepas log masuk

Usually, JavaScript code starts with the tag <span class="code-keyword">script language=<span class="code-string">"</span><span class="code-string">JavaScript"</span><span class="code-keyword">></span></span> and ends with the tag <span class="code-keyword">/script<span class="code-keyword">></span></span>. The code placed between <span class="code-keyword">head<span class="code-keyword">></span></span> and <span class="code-keyword">/head<span class="code-keyword">></span></span>. Sometimes, people embed the code in the <span class="code-keyword">body<span class="code-keyword">></span></span> tags:

<span class="code-keyword"><span class="code-leadattribute">html</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">head</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">head</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">body</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">script</span><span class="code-keyword">></span> 
.....// The code embedded <span class="code-keyword">in</span> the  tags. 
<span class="code-keyword"></span><span class="code-leadattribute">script</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">body</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">html</span><span class="code-keyword">></span></span></span></span></span></span></span></span>
Salin selepas log masuk

Why do we place JavaScript code inside comment fields <span class="code-keyword"><!--</SPAN><SPAN class=code-keyword></SPAN> and <SPAN class=code-comment>//</SPAN><SPAN class=code-comment>--></span>? It's for ensuring that the Script is not displayed by old browsers that do not support JavaScript. This is optional, but considered good practice. The LANGUAGE attribute also is optional, but recommended. You may specify a particular version of JavaScript:

<span class="code-keyword">script language=<span class="code-string">"</span><span class="code-string">JavaScript1.2"</span><span class="code-keyword">></span></span>
Salin selepas log masuk

You can use another attribute SRC to include an external file containing JavaScript code:

<span class="code-keyword">script language=<span class="code-string">"</span><span class="code-string">JavaScript"</span> src=<span class="code-string">"</span><span class="code-string">hello.js"</span><span class="code-keyword">></span><span class="code-keyword">/script<span class="code-keyword">></span></span></span>
Salin selepas log masuk

For example, shown below is the code of the external file hello.js:

document.write(<span class="code-string">"</span><span class="code-string">Hello World!"</span>)
Salin selepas log masuk

The external file is simply a text file containing JavaScript code with the file name extension ".js". Note:

  1. Including an external file only functions reliably across platforms in the version 4 browsers.
  2. The code can't include tags <span class="code-keyword">script language...<span class="code-keyword">></span></span> and <span class="code-keyword">/script<span class="code-keyword">></span></span>, or you will get an error message.

Back to top

write and writeln

In order to output text in JavaScript you must use write() or writeln(). Here's an example:

<span class="code-keyword">HTML<span class="code-keyword">></span> 
<span class="code-keyword">HEAD<span class="code-keyword">></span> 
<span class="code-keyword">TITLE<span class="code-keyword">></span> Welcome to my site<span class="code-keyword">/TITLE<span class="code-keyword">></span><span class="code-keyword">/HEAD<span class="code-keyword">></span> 
<span class="code-keyword">BODY<span class="code-keyword">></span> 
<span class="code-keyword">SCRIPT LANGUAGE=<span class="code-string">"</span><span class="code-string">JAVASCRIPT"</span><span class="code-keyword">></span> 
<span class="code-keyword">!-- 
document.write(<span class="code-string">"</span><span class="code-string">Welcome to my site!"</span>); 
<span class="code-comment">//</span><span class="code-comment"> --> 
</span> 
<span class="code-keyword">/SCRIPT<span class="code-keyword">></span> 
<span class="code-keyword">/BODY<span class="code-keyword">></span> 
<span class="code-keyword">/HTML<span class="code-keyword">></span></span></span></span></span></span></span></span></span></span></span></span>
Salin selepas log masuk

Note: the document object write is in lowercase as JavaScript is case sensitive. The difference between write and writeln is: write just outputs a text, writeln outputs the text and a line break.


Back to top

Document object

The document object is one of the most important objects of JavaScript. Shown below is a very simple JavaScript code:

document.write(<span class="code-string">"</span><span class="code-string">Hi there."</span>)
Salin selepas log masuk

In this code, document is the object. write is the method of this object. Let's have a look at some of the other methods that the document object possesses.

lastModified

You can always include the last update date on your page by using the following code:
 
<span class="code-keyword">script language=<span class="code-string">"</span><span class="code-string">JavaScript"</span><span class="code-keyword">></span> 
document.write(<span class="code-string">"</span><span class="code-string">This page created by John N. Last update:"</span> + document.lastModified); 
<span class="code-keyword">/script<span class="code-keyword">></span> </span></span>
Salin selepas log masuk

All you need to do here is use the lastModified property of the document. Notice that we used + to put together This page created by John N. Last update: and document.lastModified.

bgColor and fgColor

Lets try playing around with bgColor and fgColor:

<span class="code-keyword">script<span class="code-keyword">></span> 
document.bgColor=<span class="code-string">"</span><span class="code-string">black"</span> 
document.fgColor=<span class="code-string">"</span><span class="code-string">#336699"</span> 
<span class="code-keyword">/script<span class="code-keyword">></span> </span></span>
Salin selepas log masuk

Back to top

Message Box

alert

There are three message boxes: alert, confirm, and prompt. Let's look at the first one:
<span class="code-keyword">body<span class="code-keyword">></span> 
<span class="code-keyword">script<span class="code-keyword">></span> 
window.alert(<span class="code-string">"</span><span class="code-string">Welcome to my site!"</span>) 
<span class="code-keyword">/script<span class="code-keyword">></span> 
<span class="code-keyword">/body<span class="code-keyword">></span></span></span></span></span>
Salin selepas log masuk

You can put whatever you want inside the quotation marks.

confirm

An example for confirm box:
 
window.confirm(<span class="code-string">"</span><span class="code-string">Are you sure you want to quit?"</span>) 
Salin selepas log masuk

prompt

Prompt box is used to allow a user to enter something according the promotion:

 
window.prompt(<span class="code-string">"</span><span class="code-string">please enter user name"</span>) 
Salin selepas log masuk

In all our examples above, we wrote the box methods as window.alert(). Actually, we could simply write the following instead as:

alert() 
confirm() 
prompt()
Salin selepas log masuk

Back to top

Variables and Conditions

Let's see an example:

<span class="code-keyword">script<span class="code-keyword">></span> 
var x=window.confirm(<span class="code-string">"</span><span class="code-string">Are you sure you want to quit"</span>) 

<span class="code-keyword">if</span> (x) 
  window.alert(<span class="code-string">"</span><span class="code-string">Thank you."</span>) 
<span class="code-keyword">else</span> 
  window.alert(<span class="code-string">"</span><span class="code-string">Good choice."</span>) 
<span class="code-keyword">/script<span class="code-keyword">></span></span></span>
Salin selepas log masuk

There are several concepts that we should know. First of all, var x= is a variable declaration. If you want to create a variable, you must declare the variable using the var statement. x will get the result, namely, <span class="code-keyword">true</span> or <span class="code-keyword">false</span>. Then we use a condition statement <span class="code-keyword">if</span> <span class="code-keyword">else</span> to give the script the ability to choose between two paths, depending on this result (condition for the following action). If the result is true (the user clicked "ok"), "Thank you" appears in the window box. If the result is false (the user clicked "cancel"), "Good choice" appears in the window box instead. So we can make more complex boxes using var, <span class="code-keyword">if</span> and those basic methods.

<span class="code-keyword">script<span class="code-keyword">></span> 
var y=window.prompt(<span class="code-string">"</span><span class="code-string">please enter your name"</span>) 
window.alert(y) 
<span class="code-keyword">/script<span class="code-keyword">></span></span></span>
Salin selepas log masuk

Another example:

<span class="code-keyword"><span class="code-leadattribute">html</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-leadattribute">head</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">script</span><span class="code-keyword">></span> 
var x=confirm(<span class="code-string">"</span><span class="code-string">Are you sure you want to quit?"</span>) 
<span class="code-keyword">if</span> (!x) 
  window.location=<span class="code-string">"</span><span class="code-string">http://www.yahoo.com"</span> 
<span class="code-keyword"></span><span class="code-leadattribute">script</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">head</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">body</span><span class="code-keyword">></span> 
Welcome to my website!. 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">body</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">html</span><span class="code-keyword">></span></span></span></span></span></span></span></span>
Salin selepas log masuk

If you click "cancel", it will take you to yahoo, and clicking ok will continue with the loading of the current page "Welcome to my website!". Note:<span class="code-keyword">if</span> (!x)means: if click "cancel". In JavaScript, the exclamation mark ! means: "none".


Back to top

Function

Functions are chunks of code.Let's create a simple function:

function test() 
{ 
  document.write(<span class="code-string">"</span><span class="code-string">Hello can you see me?"</span>) 
}
Salin selepas log masuk

Note that if only this were within your script><span class="code-keyword">/script<span class="code-keyword">></span></span> tags, you will not see "Hello can you see me?" on your screen because functions are not executed by themselves until you call upon them. So we should do something:

function test() 
{ 
  document.write(<span class="code-string">"</span><span class="code-string">Hello can you see me?"</span>) 
} 
test() 
Salin selepas log masuk

Last linetest() calls the function, now you will see the words "Hello can you see me?".


Back to top

Event handler

What are event handlers? They can be considered as triggers that execute JavaScript when something happens, such as click or move your mouse over a link, submit a form etc.

onClick

onClick handlers execute something only when users click on buttons, links, etc. Let's see an example:

<span class="code-keyword">script<span class="code-keyword">></span> 
function ss() 
{ 
alert(<span class="code-string">"</span><span class="code-string">Thank you!"</span>) 
} 
<span class="code-keyword">/script<span class="code-keyword">></span> 
<span class="code-keyword">form<span class="code-keyword">></span> 
<span class="code-keyword">input type=<span class="code-string">"</span><span class="code-string">button"</span> value=<span class="code-string">"</span><span class="code-string">Click here"</span> onclick=<span class="code-string">"</span><span class="code-string">ss()"</span><span class="code-keyword">></span> 
<span class="code-keyword">/form<span class="code-keyword">></span></span></span></span></span></span>
Salin selepas log masuk

The function ss() is invoked when the user clicks the button. Note: Event handlers are not added inside the <span class="code-keyword">script<span class="code-keyword">></span></span> tags, but rather, inside the html tags.

onLoad

The onload event handler is used to call the execution of JavaScript after loading:

<span class="code-keyword">body onload=<span class="code-string">"</span><span class="code-string">ss()"</span><span class="code-keyword">></span> 
<span class="code-keyword">frameset onload=<span class="code-string">"</span><span class="code-string">ss()"</span><span class="code-keyword">></span> 
<span class="code-keyword">img src=<span class="code-string">"</span><span class="code-string">whatever.gif"</span> onload=<span class="code-string">"</span><span class="code-string">ss()"</span><span class="code-keyword">></span></span></span></span>
Salin selepas log masuk

onMouseover,onMouseout

These handlers are used exclusively with links.

<span class="code-keyword">a href=<span class="code-string">"</span><span class="code-string">#"</span> onMouseOver=<span class="code-string">"</span><span class="code-string">document.write('Hi, nice to see you!"</span><span class="code-keyword">></span>Over Here!<span class="code-keyword">/a<span class="code-keyword">></span> 
<span class="code-keyword">a href=<span class="code-string">"</span><span class="code-string">#"</span> onMouseOut=<span class="code-string">"</span><span class="code-string">alert('Good try!')"</span><span class="code-keyword">></span>Get Out Here!<span class="code-keyword">/a<span class="code-keyword">></span></span></span></span></span>
Salin selepas log masuk

onUnload

onunload executes JavaScript while someone leaves the page. For example to thank users.

<span class="code-keyword">body onunload=<span class="code-string">"</span><span class="code-string">alert('Thank you for visiting us. See you soon')"</span><span class="code-keyword">></span></span>
Salin selepas log masuk

Handle multiple actions

How do you have an event handler call multiple functions/statements? That's simple. You just need to embed the functions inside the event handler as usual, but separate each of them using a semicolon:

<span class="code-keyword">form<span class="code-keyword">></span> 
<span class="code-keyword">input type=<span class="code-string">"</span><span class="code-string">button"</span> value=<span class="code-string">"</span><span class="code-string">Click here!"</span> onClick=<span class="code-string">"</span><span class="code-string">alert('Thanks 
for visiting my site!');window.location='http://www.yahoo.com'"</span><span class="code-keyword">></span> 
<span class="code-keyword">/form<span class="code-keyword">></span></span></span></span>
Salin selepas log masuk

Back to top

Form

Let's say you have a form like this:

<span class="code-keyword">form name=<span class="code-string">"</span><span class="code-string">aa"</span><span class="code-keyword">></span> 
<span class="code-keyword">input type=<span class="code-string">"</span><span class="code-string">text"</span> size=<span class="code-string">"</span><span class="code-string">10"</span> value=<span class="code-string">"</span><span class="code-string">"</span> name=<span class="code-string">"</span><span class="code-string">bb"</span><span class="code-keyword">></span><span class="code-keyword">br<span class="code-keyword">></span> 
<span class="code-keyword">input type=<span class="code-string">"</span><span class="code-string">button"</span> 
value=<span class="code-string">"</span><span class="code-string">Click Here"</span>onclick=<span class="code-string">"</span><span class="code-string">alert(document.aa.bb.value)"</span><span class="code-keyword">></span> 
<span class="code-keyword">/form<span class="code-keyword">></span></span></span></span></span></span>
Salin selepas log masuk

Notice that we gave the names to the form and the element. So JavaScript can gain access to them.

onBlur

If you want to get information from users and want to check each element (ie: user name, password, email) individually, and alert the user to correct the wrong input before moving on, you can use onBlur. Let's see how onblur works:

<span class="code-keyword"><span class="code-leadattribute">html</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-leadattribute">head</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-leadattribute">script</span><span class="code-keyword">></span> 
<span class="code-keyword">function</span> emailchk() 
{ 
var x=document.feedback.email.value 
<span class="code-keyword">if</span> (x.indexOf(<span class="code-string">"</span><span class="code-string">@"</span>)==-1) 
{ 
  alert(<span class="code-string">"</span><span class="code-string">It seems you entered an invalid email address."</span>) 
  document.feedback.email.focus() 
} 
} 
<span class="code-keyword"></span><span class="code-leadattribute">script</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">head</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-leadattribute">body</span><span class="code-keyword">></span></span></span></span></span></span>
Salin selepas log masuk
<span class="code-keyword">form 
name=<span class="code-string">"</span><span class="code-string">feedback"</span><span class="code-keyword">></span> 
Email:<span class="code-keyword">input type=<span class="code-string">"</span><span class="code-string">text"</span> size=<span class="code-string">"</span><span class="code-string">20"</span> name=<span class="code-string">"</span><span class="code-string">email"</span> 
onblur=<span class="code-string">"</span><span class="code-string">emailchk()"</span><span class="code-keyword">></span><span class="code-keyword">br<span class="code-keyword">></span> 
Comment: <span class="code-keyword">textarea name=<span class="code-string">"</span><span class="code-string">comment"</span> rows=<span class="code-string">"</span><span class="code-string">2"</span> cols=<span class="code-string">"</span><span class="code-string">20"</span><span class="code-keyword">></span><span class="code-keyword">/textarea<span class="code-keyword">></span><span class="code-keyword">br<span class="code-keyword">></span> 
<span class="code-keyword">input type=<span class="code-string">"</span><span class="code-string">submit"</span> value=<span class="code-string">"</span><span class="code-string">Submit"</span><span class="code-keyword">></span> 
<span class="code-keyword">/form<span class="code-keyword">></span> 
<span class="code-keyword">/body<span class="code-keyword">></span><span class="code-keyword">/html<span class="code-keyword">></span></span></span></span></span></span></span></span></span></span></span>
Salin selepas log masuk

If you enter an email address without the @, you'll get an alert asking you to re-enter the data. What is: x.indexOf(@)==-<span class="code-digit">1</span>? This is a method that JavaScript can search every character within a string and look for what we want. If it finds it will return the position of the char within the string. If it doesn't, it will return -1. Therefore, x.indexOf(<span class="code-string">"</span><span class="code-string">@"</span>)==-<span class="code-digit">1</span> basically means: "if the string doesn't include @, then:

alert(<span class="code-string">"</span><span class="code-string">It seems you entered an invalid email address."</span>) 
document.feedback.email.focus()
Salin selepas log masuk

What's focus()? This is a method of the text box, which basically forces the cursor to be at the specified text box. onsubmit Unlike onblur, onsubmit handler is inserted inside the <span class="code-keyword">form<span class="code-keyword">></span></span> tag, and not inside any one element. Lets do an example:

<span class="code-keyword">script<span class="code-keyword">></span> 
<span class="code-keyword">!-- 
function validate() 
{ 
<span class="code-keyword">if</span>(document.login.userName.value==<span class="code-string">"</span><span class="code-string">"</span>) 
{ 
  alert (<span class="code-string">"</span><span class="code-string">Please enter User Name"</span>) 
  <span class="code-keyword">return</span> <span class="code-keyword">false</span> 
} 
<span class="code-keyword">if</span>(document.login.password.value==<span class="code-string">"</span><span class="code-string">"</span>) 
{ 
  alert (<span class="code-string">"</span><span class="code-string">Please enter Password"</span>) 
  <span class="code-keyword">return</span> <span class="code-keyword">false</span> 
} 
} 
<span class="code-comment">//</span><span class="code-comment">--> 
</span> 
<span class="code-keyword">/script<span class="code-keyword">></span></span></span></span>
Salin selepas log masuk
<span class="code-keyword">form name=<span class="code-string">"</span><span class="code-string">login"</span> onsubmit=<span class="code-string">"</span><span class="code-string">return validate()"</span><span class="code-keyword">></span> 
<span class="code-keyword">input type=<span class="code-string">"</span><span class="code-string">text"</span> size=<span class="code-string">"</span><span class="code-string">20"</span> name=<span class="code-string">"</span><span class="code-string">userName"</span><span class="code-keyword">></span> 
<span class="code-keyword">input type=<span class="code-string">"</span><span class="code-string">text"</span> size=<span class="code-string">"</span><span class="code-string">20"</span> name=<span class="code-string">"</span><span class="code-string">password"</span><span class="code-keyword">></span> 
<span class="code-keyword">input type=<span class="code-string">"</span><span class="code-string">submit"</span> name=<span class="code-string">"</span><span class="code-string">submit"</span> value=<span class="code-string">"</span><span class="code-string">Submit"</span><span class="code-keyword">></span> 
<span class="code-keyword">/form<span class="code-keyword">></span></span></span></span></span></span>
Salin selepas log masuk

Note:
<span class="code-keyword">if</span>(document.login.userName.value==""). This means "If the box named userName of the form named login contains nothing, then...". return false. This is used to stop the form from submitting. By default, a form will return true if submitting. <span class="code-keyword">return</span> validate() That means, "if submitting, then call the function validate()<span class="code-string">"</span><span class="code-string"></span>.

Protect a file by using Login

Let's try an example

<span class="code-keyword"><span class="code-leadattribute">html</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-leadattribute">head</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">SCRIPT</span> <span class="code-attribute">Language</span><span class="code-keyword">="</span><span class="code-keyword">JavaScript"</span><span class="code-keyword">></span> 
<span class="code-keyword">function</span> checkLogin(x) 
{ 
<span class="code-keyword">if</span> ((x.id.value != <span class="code-string">"</span><span class="code-string">Sam"</span>)||(x.pass.value !=<span class="code-string">"</span><span class="code-string">Sam123"</span>)) 
{ 
  alert(<span class="code-string">"</span><span class="code-string">Invalid Login"</span>); 
  <span class="code-keyword">return</span> <span class="code-keyword">false</span>; 
} 
<span class="code-keyword">else</span> 
  location=<span class="code-string">"</span><span class="code-string">main.htm"</span> 
} 
<span class="code-keyword"></span><span class="code-leadattribute">script</span><span class="code-keyword">></span></span></span></span>
Salin selepas log masuk
<span class="code-keyword">/head<span class="code-keyword">></span><span class="code-keyword">body<span class="code-keyword">></span> 
<span class="code-keyword">form<span class="code-keyword">></span> 
<span class="code-keyword">p<span class="code-keyword">></span>UserID:<span class="code-keyword">input type=<span class="code-string">"</span><span class="code-string">text"</span> name=<span class="code-string">"</span><span class="code-string">id"</span><span class="code-keyword">></span><span class="code-keyword">/p<span class="code-keyword">></span> 
<span class="code-keyword">p<span class="code-keyword">></span>Password:<span class="code-keyword">input type=<span class="code-string">"</span><span class="code-string">password"</span> name=<span class="code-string">"</span><span class="code-string">pass"</span><span class="code-keyword">></span><span class="code-keyword">/p<span class="code-keyword">></span> 
<span class="code-keyword">p<span class="code-keyword">></span><span class="code-keyword">input type=<span class="code-string">"</span><span class="code-string">button"</span> value=<span class="code-string">"</span><span class="code-string">Login"</span> onClick=<span class="code-string">"</span><span class="code-string">checkLogin(this.form)"</span><span class="code-keyword">></span><span class="code-keyword">/p<span class="code-keyword">></span> 
<span class="code-keyword">/form<span class="code-keyword">></span> 
<span class="code-keyword">/body<span class="code-keyword">></span><span class="code-keyword">/html<span class="code-keyword">></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
Salin selepas log masuk

|| means "or", and ,!= indicates "not equal". So we can explain the script: "If the id does not equal 'Sam', or the password does not equal 'Sam123', then show an alert ('Invalid Login') and stop submitting. Else, open the page 'main.htm'".


Back to top

Link

In most cases, a form can be repaced by a link:

<span class="code-keyword">a href=<span class="code-string">"</span><span class="code-string">JavaScript:window.location.reload()"</span><span class="code-keyword">></span>Click to reload!<span class="code-keyword">/a<span class="code-keyword">></span></span></span>
Salin selepas log masuk

More examples:

<span class="code-keyword">a href=<span class="code-string">"</span><span class="code-string">#"</span> onClick=<span class="code-string">"</span><span class="code-string">alert('Hello, world!')"</span><span class="code-keyword">></span>Click me to say Hello<span class="code-keyword">/a<span class="code-keyword">></span><span class="code-keyword">br<span class="code-keyword">></span></span></span></span>
Salin selepas log masuk
<span class="code-keyword">a href=<span class="code-string">"</span><span class="code-string">#"</span> onMouseOver=<span class="code-string">"</span><span class="code-string">location='main.htm'"</span><span class="code-keyword">></span>Mouse over to see Main Page<span class="code-keyword">/a<span class="code-keyword">></span></span></span>
Salin selepas log masuk

Back to top

Date

Let's see an example:

<span class="code-keyword">HTML<span class="code-keyword">></span><span class="code-keyword">HEAD<span class="code-keyword">></span><span class="code-keyword">TITLE<span class="code-keyword">></span>Show 
Date<span class="code-keyword">/TITLE<span class="code-keyword">></span><span class="code-keyword">/HEAD<span class="code-keyword">></span> 
<span class="code-keyword">BODY<span class="code-keyword">></span> 
<span class="code-keyword">SCRIPT LANGUAGE=<span class="code-string">"</span><span class="code-string">JavaScript"</span><span class="code-keyword">></span> 
var x= <span class="code-keyword">new</span> Date(); 
document.write (x); 
<span class="code-keyword">/SCRIPT<span class="code-keyword">></span> 
<span class="code-keyword">/BODY<span class="code-keyword">></span><span class="code-keyword">/HTML<span class="code-keyword">></span></span></span></span></span></span></span></span></span></span></span>
Salin selepas log masuk

To activate a Date Object, you can do this: var x=new Date(). Whenever you want to create an instance of the date object, use this important word: new followed by the object name().

Dynamically display different pages

You can display different pages according to the different time. Here is an example:

var banTime= <span class="code-keyword">new</span> Date() 
var ss=banTime.getHours() 
<span class="code-keyword">if</span> (ss<span class="code-keyword">=12) 
  document.write(<span class="code-string">"</span><span class="code-string"><img  src="banner1.gif" alt="国外的为初学者写的JavaScript教程_基础知识" >"</span>) 
<span class="code-keyword">else</span> 
  document.write(<span class="code-string">"</span><span class="code-string"><img  src="banner2.gif" alt="国外的为初学者写的JavaScript教程_基础知识" >"</span>) </span>
Salin selepas log masuk
Date object
Methods
getDate
getTime
getTimezoneOffset
getDay
getMonth
getYear
getSeconds
getMinutes
getHours

 

Window

Open a window

To open a window, simply use the method "window.open()":

<span class="code-keyword">form<span class="code-keyword">></span> 
<span class="code-keyword">input type=<span class="code-string">"</span><span class="code-string">button"</span> value=<span class="code-string">"</span><span class="code-string">Click here to see"</span> onclick=<span class="code-string">"</span><span class="code-string">window.open('test.htm')"</span><span class="code-keyword">></span> 
<span class="code-keyword">/form<span class="code-keyword">></span> </span></span></span>
Salin selepas log masuk

You can replace test.htm with any URL, for example, with http:<span class="code-comment">//</span><span class="code-comment">www.yahoo.com</span>.

Size, toolbar, menubar, scrollbars, location, status

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(<span class="code-string">"</span><span class="code-string">URL"</span>,<span class="code-string">"</span><span class="code-string">name"</span>,<span class="code-string">"</span><span class="code-string">attributes"</span>)
Salin selepas log masuk

For example:

<span class="code-keyword">form<span class="code-keyword">></span> 
<span class="code-keyword">input type=<span class="code-string">"</span><span class="code-string">button"</span> value=<span class="code-string">"</span><span class="code-string">Click here to see"</span> 
onclick=<span class="code-string">"</span><span class="code-string">window.open('page2.htm','win1','width=200,height=200,menubar')"</span><span class="code-keyword">></span> 
<span class="code-keyword">/form<span class="code-keyword">></span> </span></span></span>
Salin selepas log masuk

Another example with no attributes turned on, except the size changed:

<span class="code-keyword">form<span class="code-keyword">></span> 
<span class="code-keyword">input type=<span class="code-string">"</span><span class="code-string">button"</span> value=<span class="code-string">"</span><span class="code-string">Click here to see"</span> 
onclick=<span class="code-string">"</span><span class="code-string">window.open('page2.htm','win1','width=200,height=200')"</span><span class="code-keyword">></span> 
<span class="code-keyword">/form<span class="code-keyword">></span></span></span></span>
Salin selepas log masuk

Here is the complete list of attributes you can add:

width height toolbar
location directories status
scrollbars resizable menubar

Reload

To reload a window, use this method:

window.location.reload()
Salin selepas log masuk

Close Window

Your can use one of the codes shown below:

<span class="code-keyword">form<span class="code-keyword">></span> 
<span class="code-keyword">input type=<span class="code-string">"</span><span class="code-string">button"</span> value=<span class="code-string">"</span><span class="code-string">Close Window"</span> onClick=<span class="code-string">"</span><span class="code-string">window.close()"</span><span class="code-keyword">></span> 
<span class="code-keyword">/form<span class="code-keyword">></span> 
<span class="code-keyword">a href=<span class="code-string">"</span><span class="code-string">javascript:window.close()"</span><span class="code-keyword">></span>Close Window<span class="code-keyword">/a<span class="code-keyword">></span></span></span></span></span></span>
Salin selepas log masuk

Loading

The basic syntax when loading new content into a window is:

window.location=<span class="code-string">"</span><span class="code-string">test.htm"</span>
Salin selepas log masuk

This is the same as

<span class="code-keyword">a href=<span class="code-string">"</span><span class="code-string">test.htm>Try this </span></span>
Salin selepas log masuk

Let's provide an example, where a confirm box will allow users to choose between going to two places:

<span class="code-keyword">script<span class="code-keyword">></span> 
<span class="code-keyword">!-- 
function ss() 
{ 
var ok=confirm(<span class="code-string">'</span><span class="code-string">Click "OK" to go to yahoo, "CANCEL" to go to hotmail'</span>) 
<span class="code-keyword">if</span> (ok) 
location=<span class="code-string">"</span><span class="code-string">http://www.yahoo.com"</span> 
<span class="code-keyword">else</span> 
location=<span class="code-string">"</span><span class="code-string">http://www.hotmail.com"</span> 
} 
<span class="code-comment">//</span><span class="code-comment">--> 
</span> 
<span class="code-keyword">/script<span class="code-keyword">></span></span></span></span>
Salin selepas log masuk

Remote Control Window

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(<span class="code-string">'</span><span class="code-string">test.htm'</span>,<span class="code-string">'</span><span class="code-string">'</span>,<span class="code-string">'</span><span class="code-string">width=200,height=200'</span>)
Salin selepas log masuk

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:

<span class="code-keyword"><span class="code-leadattribute">html</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-leadattribute">head</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-leadattribute">title</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">title</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">head</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">body</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">form</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">input</span> <span class="code-attribute">type</span><span class="code-keyword">="</span><span class="code-keyword">button"</span> <span class="code-attribute">value</span><span class="code-keyword">="</span><span class="code-keyword">Open another page"</span> 
<span class="code-attribute">onClick</span><span class="code-keyword">="</span><span class="code-keyword">aa=window.open('test.htm','','width=200,height=200')"</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">input</span> <span class="code-attribute">type</span><span class="code-keyword">="</span><span class="code-keyword">radio"</span> <span class="code-attribute">name</span><span class="code-keyword">="</span><span class="code-keyword">x"</span> <span class="code-attribute">onClick</span><span class="code-keyword">="</span><span class="code-keyword">aa.document.bgColor='red'"</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">input</span> <span class="code-attribute">type</span><span class="code-keyword">="</span><span class="code-keyword">radio"</span> <span class="code-attribute">name</span><span class="code-keyword">="</span><span class="code-keyword">x"</span> <span class="code-attribute">onClick</span><span class="code-keyword">="</span><span class="code-keyword">aa.document.bgColor='green'"</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">input</span> <span class="code-attribute">type</span><span class="code-keyword">="</span><span class="code-keyword">radio"</span> <span class="code-attribute">name</span><span class="code-keyword">="</span><span class="code-keyword">x"</span> <span class="code-attribute">onClick</span><span class="code-keyword">="</span><span class="code-keyword">aa.document.bgColor='yellow'"</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">form</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">body</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">html</span><span class="code-keyword">></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
Salin selepas log masuk

opener

Using <span class="code-string">"</span><span class="code-string">opener"</span> property, we can access the main window from the newly opened window.

Let's create Main page:

<span class="code-keyword"><span class="code-leadattribute">html</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">head</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">title</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">title</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">head</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">body</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">form</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">input</span> <span class="code-attribute">type</span><span class="code-keyword">="</span><span class="code-keyword">button"</span> <span class="code-attribute">value</span><span class="code-keyword">="</span><span class="code-keyword">Open another page"</span> 
<span class="code-attribute">onClick</span><span class="code-keyword">="</span><span class="code-keyword">aa=window.open('test.htm','','width=100,height=200')"</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">form</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">body</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">html</span><span class="code-keyword">></span></span></span></span></span></span></span></span></span></span></span></span>
Salin selepas log masuk

Then create Remote control page (in this example, that is test.htm):

<span class="code-keyword"><span class="code-leadattribute">html</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">head</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">title</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">title</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">script</span><span class="code-keyword">></span> 
<span class="code-keyword">function</span> remote(url){ 
window.opener.location=url 
} 
<span class="code-keyword"></span><span class="code-leadattribute">script</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">head</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">body</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">p</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-leadattribute">a</span> <span class="code-attribute">href</span><span class="code-keyword">="</span><span class="code-keyword">#"</span> <span class="code-attribute">onClick</span><span class="code-keyword">="</span><span class="code-keyword">remote('file1.htm')"</span><span class="code-keyword">></span>File 
1<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">a</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">p</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">p</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-leadattribute">a</span> <span class="code-attribute">href</span><span class="code-keyword">="</span><span class="code-keyword">#"</span> <span class="code-attribute">onClick</span><span class="code-keyword">="</span><span class="code-keyword">remote('file2.htm')"</span><span class="code-keyword">></span>File 
2<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">a</span><span class="code-keyword">></span><span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">p</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">body</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">html</span><span class="code-keyword">></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
Salin selepas log masuk

Try it now!


Frame

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:

<span class="code-keyword"><span class="code-leadattribute">html</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">frameset</span> <span class="code-attribute">cols</span><span class="code-keyword">="</span><span class="code-keyword">150,*"</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">frame</span> <span class="code-attribute">src</span><span class="code-keyword">="</span><span class="code-keyword">page1.htm"</span> <span class="code-attribute">name</span><span class="code-keyword">="</span><span class="code-keyword">frame1"</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">frame</span> <span class="code-attribute">src</span><span class="code-keyword">="</span><span class="code-keyword">page2.htm"</span> <span class="code-attribute">name</span><span class="code-keyword">="</span><span class="code-keyword">frame2"</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">frameset</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">html</span><span class="code-keyword">></span></span></span></span></span></span></span>
Salin selepas log masuk

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:

 
<span class="code-keyword"><span class="code-leadattribute">html</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">body</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">h2</span><span class="code-keyword">></span>This is page 1 <span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">h2</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-leadattribute">a</span> <span class="code-attribute">href</span><span class="code-keyword">="</span><span class="code-keyword">page3.htm"</span> 
<span class="code-attribute">onClick</span><span class="code-keyword">="</span><span class="code-keyword">parent.frame2.location='page4.htm'"</span><span class="code-keyword">></span>Click Here<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">a</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">body</span><span class="code-keyword">></span> 
<span class="code-keyword"><span class="code-keyword">/</span><span class="code-leadattribute">html</span><span class="code-keyword">></span></span></span></span></span></span></span></span></span>
Salin selepas log masuk

Notice: You should use <span class="code-string">"</span><span class="code-string">parent.frameName.location"</span> to access another frame. "parent" standards for the parent frame containing the frameset code.


 

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan