First, understand JavaScript
JavaScript is a scripting language that supports the development of client and server-side components of Web applications and is widely used in web systems.
1. What is JavaScript
JavaScript, developed by Netscape Communication Corporation (Netscape), is a basic object and event-driven, interpreted scripting language with security performance.
Not only can it be used to write client-side scripts, interpreted by a Web browser, but it is also possible to write scripted programs executed on the server, process user-submitted information on the server side, and return processing results dynamically to the browser.
2. JavaScript features
JavaScript is a popular scripting language for creating Web effects, which is interpreted by the client browser and can be applied to PHP, ASP, JSP, and ASP. At the same time, the more popular Ajax is based on JavaScript,
This shows that mastering and applying JavaScript is very important for web developers.
It is mainly applied to the following aspects:
(1), in, facilitate the communication between the site and the user, timely response to the user's operation, the submitted form to do an instant check, such as verifying that the form element is empty, verify that the form element is numeric, detect whether the form element input error, and so on.
(2), the use of JS script to create Web site effects, such as dynamic menus, floating ads, for the page to add gorgeous dynamic effect, using Web content more rich and lively.
(3), the application of JS script, the creation of complex web content, such as open a new window loading Web pages.
(4), the application of JS script, the user can be different events to produce different responses.
(5), the application of JS script, the production of a variety of pictures, text, mouse, animation and page effect.
(6), the application of JS script, make some small games.
Second, JavaScript language Foundation
The JS scripting language, like other languages, has its own basic data types, expressions and operators, and basic framework structures for programs.
1. JS Data type
There are 6 main types of data:
String type: One or more characters enclosed in single or double quotation marks. such as "PHP" and so on.
Numeric: includes integers or floating-point numbers (numbers containing decimal points or scientific notation) such as-128, 12.9, 6.98E6, etc.
Boolean: Boolean constants have only two states, true or false, such as Event.return Value=false
Object type: Used to specify the object in the JS program. such as Web Form elements
Null value: You can clear the contents of a variable by assigning a null value to a variable. such as A=null
Undefined: Indicates that the variable has not been assigned a value. such as Var a
1. JS variable
A variable is a named storage unit in a program, and its main function is to provide a container for data operations to hold information. Before using variables, you must define the naming rules of variables, the declaration methods of variables, and the scope of variables.
(1), naming rules for variables
You must start with a letter or underscore, and the middle can be a number, letter, or underscore.
Variable names cannot contain symbols such as spaces or plus signs, minus signs, and so on.
The variable names of JS are strictly case-sensitive.
You cannot use the keywords in JS. JS keywords such as the following table: Abstract,continue,finally,instanceof,private,this,boolean,default,float,int,public,throw,break,do, For,interface,
Return,typeof,byte,double,function,long,short,true,case,else,goto,native,static,var,catch,extends,implements, New,super,void,char,false,import,null,
Switch,while,class,final,in,package,synchronized,with
Note: Although JS variables can be arbitrarily named, but in order to use code more standardized in programming, it is best to use memory-friendly and meaningful variable names to increase the readability of the program.
(2), Declaration and assignment of variables
In JS, it is common to declare a variable before using it, but sometimes the variable does not have to be declared before it is used to determine the data type to which it belongs, depending on the actual action of the variable. All JS variables are declared by the keyword var.
The syntax is as follows:
Varexample;
You can also assign a variable to a variable while declaring it:
varexample=100;
It is recommended that the reader declare the variable before it is used, because the greatest benefit of declaring a variable is to be able to find errors in the code in a timely manner. Since JS is dynamically compiled,
Dynamic compilation is not an easy way to find errors in your code, especially in terms of variable naming.
The following rules are followed when declaring variables:
You can declare multiple variables at the same time using a keyword VAR, for example:
Var i,j;
You can assign a variable while declaring it, which is initialization: for example:
Var i=1;j=100;
If a variable is declared only and is not assigned a value, its value defaults to undefined.
In JS, you can use semicolons to represent the end of a statement, and if each statement is in a different row, the semicolon can be omitted if multiple statements are on the same line,
Then the semicolon cannot be omitted. We recommend that you do not omit the semicolon to develop good programming habits.
Cases:
Var i=100;
Var str= "There is a way to walk through the always remembered." ”;
Var content=true;
In the process of program development, you can use the VAR statement to declare the same variable multiple times, if the duplicate declared variable already has an initial value, then the declaration at this time is equivalent to the variable re-assignment.
2, JS's comments
In JS, there are 2 kinds of annotation methods used:
(1), single-line comment
Single-line comments are identified using//. The text after the symbol cannot be interpreted by the program.
(2), multi-line Comment
Multi-line annotations are identified using "/* */". The text in the middle of the "/* ... * *" symbol is not interpreted by the program.
Single-line comments can be nested in multiline comments, but multiple lines of comments cannot be nested. Because the first/* will match the first */in the back, so that subsequent comments do not work, or even cause a program error.
In addition, JS can also recognize the beginning of the HTML comment <!--JS will see it as a single-line comment end, such as using//the same. But JS does not recognize the end part of the HTML comment-->.
The main reason for this phenomenon is: in JS, if the first line starts with the <!--and the last line ends, then the program is included in a full HTML comment.
will be ignored by browsers that do not support JS and cannot be displayed. If the first line starts with the <!--and the last line ends with//-->, JS ignores both lines without ignoring the part between the two lines. In this way, you can hide the code for browsers that don't understand JS, and you don't have to hide it for browsers that understand JS.
Iii. Custom Functions
A custom function is a program unit that is named and written by the user to implement a specific function. Custom functions used by the user must be declared in advance and cannot be directly used with custom functions that have not been declared.
JS uses function to define functions, with the following syntax:
Function name (parameter) {
Return var;
}
The calling method for a custom function is:
function name ();
The parentheses must not be omitted.
Cases:
<scriptlanguage= "JavaScript" >
function Chengji (A, b) {
return a*b;
}
document.write ("The result of the output is:" +chengji (15,3));
</script>
The result after execution is:
The result of the output is: 45
Functions with the same name cannot be defined on the same page, and when a user customizes a function, the function needs to be referenced, otherwise the custom function loses its meaning.
IV. JavaScript Process Control statements
The so-called Process Control statement is to judge the values of different conditions in the statement, thus executing different statements according to different conditions. In JS, the Process Control statements can be divided into conditional statements, loop statements and jump statements.
1. Conditional statements
There are two main types of conditional control statements: One is an IF condition statement and the other is a switch multi-branch statement.
2. Circular statements
3. Jump Statement
V. JavaScript events
JS is an object-based language. One of its most basic features is the use of event-driven. Events are signals that occur when a certain action occurs, and these events can occur at any time. The action that causes the event to occur is called the trigger event.
Mouse keyboard Events
This event is triggered when the Onclick mouse clicks
Ondblclick this event when the mouse double-clicks
Onmousedown triggers this event when the mouse is pressed
Onmouseup triggers this event when the mouse is released when the mouse is pressed
Onumoseover triggers when the mouse moves over an object range
Onumosemove triggers this event when the mouse moves
Onmoseout triggers this event when the mouse leaves an object range
Onkeypress triggered when a key on the keyboard is pressed and released
Onkeydown This event is triggered when a key on the keyboard is pressed
Onkeyup triggered when a key on the keyboard is pressed and released
Page-related events
Onabort This event is triggered when a picture is interrupted by a user while downloading
This event is triggered when the Onload page content finishes (page load event)
Onresize This event is triggered when the browser's window size is changed
Onumload This event is triggered when the current page is changed
Form-related events
Onblur This event is triggered when the current element loses focus
Onchange the current element loses focus and no content changes
Onfocus This event is triggered when the current element receives focus
OnReset triggers events when the properties of reset in a form are activated
Onsubmit trigger This event when a form is submitted
Scrolling caption Events
Onbounce This event is triggered when the content within the marquee is moved outside the marquee display range
OnFinish triggers when the MARQUEE element finishes what needs to be displayed
Onstart This event is triggered when the marquee element starts displaying content
Invoking a custom function in PHP with an event in a JS script is a frequently used method in the process of program development.
(Basic) PHP interacts with JavaScript