国外的为初学者写的JavaScript教程_基础知识
- Embedding and including
- write and writeln
- Document object
- Message box
- Function
- Event handler
- Form
- Link
- Date
- Window
- Frame
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>
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>
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>
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>
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>)
The external file is simply a text file containing JavaScript code with the file name extension ".js". Note:
- Including an external file only functions reliably across platforms in the version 4 browsers.
- 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.
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>
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.
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>)
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>
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 withbgColor
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>
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>
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>)
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>)
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()
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>
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>
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>
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".
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>) }
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()
Last linetest()
calls the function, now you will see the words "Hello can you see me?".
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>
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>
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>
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>
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>
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>
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 useonBlur
. 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>
<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>
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()
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>
<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>
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>
<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>
||
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'".
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>
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>
<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>
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>
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>
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>
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>)
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>
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>
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()
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>
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>
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>
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>
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>)
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>
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>
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>
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>
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>
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.

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

Dewu APP ist derzeit eine sehr beliebte Marken-Shopping-Software, aber die meisten Benutzer wissen nicht, wie sie die Funktionen in Dewu APP verwenden. Nachfolgend finden Sie eine Zusammenfassung des Dewuduo-Tutorials Interessierte Benutzer können vorbeikommen und einen Blick darauf werfen! Tutorial zur Verwendung von Dewu [20.03.2024] Verwendung des Dewu-Ratenkaufs [20.03.2024] So erhalten Sie Dewu-Gutscheine [20.03.2024] So finden Sie den manuellen Kundendienst von Dewu [2024-03- 20] So überprüfen Sie den Abholcode von Dewu [20.03.2024] Wo Sie Dewu kaufen können [20.03.2024] So eröffnen Sie Dewu VIP [20.03.2024] So beantragen Sie die Rückgabe oder den Umtausch von Dewu

1. Öffnen Sie zunächst WeChat. 2. Klicken Sie oben rechts auf [+]. 3. Klicken Sie auf den QR-Code, um die Zahlung einzuziehen. 4. Klicken Sie auf die drei kleinen Punkte in der oberen rechten Ecke. 5. Klicken Sie auf , um die Spracherinnerung für den Zahlungseingang zu schließen.

PhotoshopCS ist die Abkürzung für Photoshop Creative Suite. Es handelt sich um eine von Adobe hergestellte Software, die häufig im Grafikdesign und in der Bildverarbeitung verwendet wird. Lassen Sie mich Ihnen heute erklären, was Photoshopcs5 ist und wie Sie Photoshopcs5 verwenden. 1. Welche Software ist Photoshop CS5? Adobe Photoshop CS5 Extended ist ideal für Profis in den Bereichen Film, Video und Multimedia, Grafik- und Webdesigner, die 3D und Animation verwenden, sowie Profis in den Bereichen Technik und Wissenschaft. Rendern Sie ein 3D-Bild und fügen Sie es zu einem zusammengesetzten 2D-Bild zusammen. Bearbeiten Sie Videos einfach

Nach dem Regen im Sommer können Sie oft ein wunderschönes und magisches besonderes Wetterbild sehen – den Regenbogen. Dies ist auch eine seltene Szene, die man in der Fotografie antreffen kann, und sie ist sehr fotogen. Für das Erscheinen eines Regenbogens gibt es mehrere Bedingungen: Erstens sind genügend Wassertröpfchen in der Luft und zweitens scheint die Sonne in einem niedrigeren Winkel. Daher ist es am einfachsten, einen Regenbogen am Nachmittag zu sehen, nachdem der Regen nachgelassen hat. Allerdings wird die Bildung eines Regenbogens stark von Wetter, Licht und anderen Bedingungen beeinflusst, sodass sie im Allgemeinen nur von kurzer Dauer ist und die beste Betrachtungs- und Aufnahmezeit sogar noch kürzer ist. Wenn Sie also auf einen Regenbogen stoßen, wie können Sie ihn dann richtig aufzeichnen und qualitativ hochwertige Fotos machen? 1. Suchen Sie nach Regenbögen. Zusätzlich zu den oben genannten Bedingungen erscheinen Regenbögen normalerweise in Richtung des Sonnenlichts, das heißt, wenn die Sonne von Westen nach Osten scheint, ist es wahrscheinlicher, dass Regenbögen im Osten erscheinen.

Das Testen eines Monitors beim Kauf ist ein wesentlicher Bestandteil, um den Kauf eines beschädigten Monitors zu vermeiden. Heute werde ich Ihnen beibringen, wie Sie den Monitor mit Software testen. Methodenschritt 1. Suchen Sie zunächst auf dieser Website nach der DisplayX-Software, laden Sie sie herunter, installieren Sie sie und öffnen Sie sie. Sie werden dann viele Erkennungsmethoden sehen, die den Benutzern zur Verfügung gestellt werden. 2. Der Benutzer klickt auf den regulären Volltest. Der erste Schritt besteht darin, die Helligkeit des Displays zu testen. Der Benutzer stellt die Anzeige so ein, dass die Kästchen deutlich sichtbar sind. 3. Klicken Sie dann mit der Maus, um den nächsten Link einzugeben. Wenn der Monitor jeden schwarzen und weißen Bereich unterscheiden kann, bedeutet dies, dass der Monitor immer noch in Ordnung ist. 4. Klicken Sie erneut mit der linken Maustaste und Sie sehen den Graustufentest des Monitors. Je glatter der Farbübergang, desto besser der Monitor. 5. Darüber hinaus haben wir in der DisplayX-Software

Mit der kontinuierlichen Weiterentwicklung von Smartphones sind die Funktionen von Mobiltelefonen immer leistungsfähiger geworden, darunter die Funktion zum Aufnehmen langer Bilder zu einer der wichtigen Funktionen, die viele Benutzer im täglichen Leben nutzen. Lange Screenshots können Benutzern dabei helfen, eine lange Webseite, Gesprächsaufzeichnung oder ein Bild gleichzeitig zu speichern, um sie einfacher anzeigen und teilen zu können. Unter vielen Mobiltelefonmarken gehören auch Huawei-Handys zu den Marken, die von den Nutzern sehr geschätzt werden, und auch ihre Funktion zum Zuschneiden langer Bilder wird hoch gelobt. In diesem Artikel erfahren Sie, wie Sie lange Bilder mit Huawei-Mobiltelefonen richtig aufnehmen, und erhalten einige Expertentipps, die Ihnen dabei helfen, Huawei-Mobiltelefone besser zu nutzen.

PHP-Tutorial: So konvertieren Sie den Int-Typ in einen String. In PHP ist das Konvertieren von Ganzzahldaten in einen String ein häufiger Vorgang. In diesem Tutorial erfahren Sie, wie Sie die in PHP integrierten Funktionen verwenden, um den Typ int in einen String zu konvertieren, und stellen gleichzeitig spezifische Codebeispiele bereit. Cast verwenden: In PHP können Sie Cast verwenden, um ganzzahlige Daten in einen String umzuwandeln. Diese Methode ist sehr einfach. Sie müssen lediglich (string) vor den ganzzahligen Daten hinzufügen, um sie in einen String umzuwandeln. Unten finden Sie einen einfachen Beispielcode

Viele Freunde wissen immer noch nicht, wie man Bilder in PS ausschneidet, deshalb erklärt der Herausgeber unten das Tutorial zum Ausschneiden von Bildern in PS. Wenn Sie es brauchen, schauen Sie es sich bitte an. Ich glaube, es wird für alle hilfreich sein. 1. Öffnen Sie zunächst das Bild, das ausgeschnitten werden soll, in PS (wie im Bild gezeigt). Klicken Sie nach dem Öffnen der Software auf das Zauberstab-Werkzeug in der linken Symbolleiste. Klicken Sie dann mit der Maus auf den Hintergrundbereich des Bildes und drücken Sie die Tastenkombination für die umgekehrte Auswahl [Strg+Umschalt+I], um den Hauptteil des Bildes auszuwählen. 3. Nachdem Sie das Motiv ausgewählt haben, drücken Sie die Tastenkombination [Strg+J], um die nächste Ebene zu kopieren. Schließen Sie dann die Hintergrundebene und das Bild wird ausgeschnitten (wie im Bild gezeigt). Oben finden Sie alle Tutorials zum Ausschneiden von Bildern in PS, die der Herausgeber mitgebracht hat. Ich hoffe, dass sie Ihnen hilfreich sein werden.
