时间:2021-07-01 10:21:17 帮助过:9人阅读
Contents
Embedding and including
write and writeln
Document object
Message box
Function
Event handler
Form
Link
Date
Window
Frame
Let's first see a simple example:
Usually, JavaScript code starts with the tag and ends with the tag script>. The code placed between and . Sometimes, people embed the code in the tags:
Why do we place JavaScript code inside comment fields
?
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:
You can use another attribute SRC to include an external file containing JavaScript code:
For example, shown below is the code of the external file hello.js :
The external file is simply a text file containing JavaScript code with the file name extension ".js".
Note:
In order to output text in JavaScript you must use write() or writeln(). Here's an example:
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.
The document object is one of the most important objects of JavaScript. Shown below is a very simple JavaScript code:
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.
+
to put together This page created by John N. Last update: and document.write.
Let's see an example:
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, true or false . Then we use a condition statement if else 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, if and those basic methods.
Another example:
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: if(!x) means: if click "cancel". In JavaScript, the exclamation mark !means: "none".
Functions are chunks of code.Let's create a simple function:
Note that if only this were within your