Source:http://www.w3school.com.cn/html
External JavaScript
You can also save the script to an external file. External files typically contain code that is used by multiple Web pages.
The file name extension for external JavaScript files is. js.
To use an external file, set the. js file in the "src" attribute of the <script> tag:
Instance
<! DOCTYPE html> src= "Myscript.js"></script></body>
It is possible to refer to a script file in
Tip: External scripts cannot contain <script> tags.
It is common practice to put functions in the
Write to document output<script> document.write("<p>My First JavaScript</p>");
</script>
WarningPlease use document.write () to write only to the document output.
If document.write is executed after the document has finished loading, the entire HTML page will be overwritten.
Manipulating HTML elements<script> document.getElementById("demo").innerHTML="My First JavaScript";
</script>
JavaScript is case sensitive.JavaScript is sensitive to capitalization.
Wrapping lines of codeYou can wrap lines of code in a text string with backslashes. The following example will display correctly:
document.write ("Hello world!");
However, you cannot fold a line like this:
document.write ("Hello world!");
JavaScript commentsSingle-line comments begin with//.
Multiline comments start with/* and end with */.
declaring (creating) JavaScript variablesWe use the VAR keyword to declare variables:
var carname;
Value = undefinedIn computer programs, variables that are not valued are often declared. A variable that is not declared with a value, whose value is actually undefined.
After the following statement has been executed, the value of the variable carname will be undefined:
var carname;
Re-declaring JavaScript variablesvar carname= "Volvo"; var carname;
JavaScript has a dynamic typeJavaScript arraysThe following code creates an array named cars:
var cars=new Array (); cars[0]= "Audi"; cars[1]= "BMW"; cars[2]= "Volvo";
or (condensed array):
var cars=new Array ("Audi", "BMW", "Volvo");
or (literal array):
Instancevar cars=["Audi", "BMW", "Volvo";
JavaScript objectsvar person={firstname: "Bill", LastName: "Gates", id:5566};
Object properties are addressed in two ways:
Instancename=person.lastname;name=person["LastName"];
JavaScript variables are objects. When you declare a variable, a new object is created.
- CamelCase, called "Camel orthography", refers to the practice of spelling compound words in English, depending on the size of the word. "Camel orthography" is divided into two kinds. The first letter is lowercase, the first letter of each word is capitalized, it is called "Small camel Orthography" (lowercamelcase); the first letter, and the initials of each of the following words, are capitalized, called "Big Camel Orthography" (uppercamelcase), also known as "Pascal orthography." "(Pascalcase). The variables are usually identified by the small hump method, which is often used for class names, function names, attributes, and namespaces.
function with return value
function myFunction () {var x=5; return x;
}
You can also use the return statement when you just want to exit the function. The return value is optional:
function MyFunction (A, B) {if (a>b) { return; } X=A+B}
Assigning a value to an undeclared JavaScript variable
If you assign a value to a variable that has not been declared, the variable is automatically declared as a global variable.
This statement:
Carname= "Volvo";
A global variable is declared carname, even if it executes within the function.
Add a method to a JavaScript object
method is simply a function attached to an object.
Methods for defining objects inside the constructor function:
function Person (firstname,lastname,age,eyecolor) {this.firstname=firstname;this.lastname=lastname;this.age=age; This.eyecolor=eyecolor;this.changename=changename;function ChangeName (name) {this.lastname=name;}}
The value of the ChangeName () function name is assigned to the LastName property of the person.
JavaScript class
JavaScript is an object-oriented language, but JavaScript does not use classes.
In JavaScript, classes are not created and objects are not created through classes (as in other object-oriented languages).
JavaScript is based on prototype, not class-based.
All JavaScript numbers are 64-bit
JavaScript is not a type language. Unlike many other programming languages, JavaScript does not define different types of numbers, such as integers, short, long, floating point, and so on.
All the numbers in JavaScript are stored as 64 bits (8 bits) of the root 10, floating-point numbers.
Precision
Integers (not using decimal or exponential notation) are up to 15 bits.
The maximum number of decimal digits is 17, but the floating-point operation is not always 100% accurate:
Octal and hexadecimal
If the prefix is 0, JavaScript interprets numeric constants as octal numbers, and if the prefixes are 0 and "X", they are interpreted as hexadecimal numbers.
1<script type= "Text/javascript" >2 3 varD=NewDate ()4 varweekday=NewArray (7)5weekday[0]= "Sunday"6weekday[1]= "Monday"7weekday[2]= "Tuesday"8weekday[3]= "Wednesday"9weekday[4]= "Thursday"Tenweekday[5]= "Friday" Oneweekday[6]= "Saturday" A -document.write ("Today is" +Weekday[d.getday ()]) - the</script>
Operation Date
By using a method for a Date object, we can easily manipulate the date.
In the following example, we set a specific date for the Date object (August 9, 2008):
var mydate=new Date () mydate. setFullYear
(2008,7,9)
Note: The parameter that represents the month is between 0 and 11. That is, if you want to set the month to August, the parameter should be 7. And this time class will automatically handle increased reduction.
Methods for REGEXP objects
The RegExp object has 3 methods: Test (), exec (), and compile ().
RegExp Object
The RegExp object represents a regular expression, which is a powerful tool for performing pattern matching on strings.
Direct volume syntax
/pattern/attributes
Syntax for creating REGEXP objects:
New RegExp (patternattributes);
Parameters
Parameter pattern is a string that specifies the pattern of a regular expression or other regular expression.
The parameter attributes is an optional string that contains the property "G", "I", and "M", respectively, for specifying global matching, case-sensitive matching, and multiline matching. The M attribute is not supported until ECMAScript is normalized. If pattern is a regular expression, not a string, you must omit the parameter.
modifier |
Description |
I |
Performs a match that is not case sensitive. |
G |
Performs a global match (finds all matches rather than stops after the first match is found). |
M |
Performs multi-line matching. |
Method |
Description |
Compile |
Compiles the regular expression. |
Exec |
Retrieves the value specified in the string. Returns the found value and determines its location. |
Test |
Retrieves the value specified in the string. Returns TRUE or FALSE. |
You can create three message boxes in JavaScript: A warning box, a confirmation box, and a prompt box.
Warning Box
Alert ("Text")
Confirmation box
If the user clicks Confirm, then the return value is true. If the user clicks Cancel, the return value is false.
Confirm ("text")
Prompt box
The prompt box is often used to prompt the user to enter a value before entering the page.
When the prompt box appears, the user needs to enter a value and then click the Confirm or Cancel button to continue the manipulation.
If the user clicks Confirm, then the return value is the value entered. If the user clicks Cancel, the return value is null.
Grammar:
Prompt ("text", "Default value")
"8-18" JS learning 01