Scripting and Javascript
Source: Shangpin China |
Type: website encyclopedia |
Time: 2015-06-17
1. Script and script language education A script is a sequence of instructions written in a specific descriptive language (script language) according to a certain format. Scripts express instructions by using the rules and syntax of applications or tools to control the reaction of various elements in the application program, and can also be composed of simple control structures (such as loop statements and selection statements). These applications or tools include web browsers, multimedia authoring tools, application macros, registry tools, etc. The batch files in the operating system are also scripts. Scripts can usually be temporarily called and executed by applications.
Scripts play a different role in each application. Various scripts are currently widely used in Beijing Website Design Because script can not only reduce the size of web pages and improve the browsing speed of web pages, but also enrich the performance of web pages, such as animation, sound, etc., to achieve various functions that HTML cannot achieve. In Microsoft Office components, we often see the tool "macro", which is actually a series of commands and instructions, to achieve the automation of task execution.
In the Internet, script refers to the program code embedded in the Web page. According to the different execution methods and locations, script can be divided into client script and server script. The client script is executed by the Web browser on the client computer, and the server script is executed by the Web server on the server computer. Script language is an interpreted language. The interpreter of client script is located in the Web browser, and the interpreter of server script is located in the Web server.
The characteristics of scripting languages are as follows:
(1) The scripting language is between HTML and C, C++, Java and other programming languages. HTML is often used to format and link text, while programming languages are often used to implement complex calculations and processing.
(2) There are many similarities between scripting language and programming language, including variables, data types, functions, etc., but the syntax and rules of programming language are more strict and complex.
(3) Script language is an interpretive language, and its programs are interpreted and executed by the corresponding script. Most programming languages are executed by compiling, while others are executed by interpreting. For example, C/C++programs are compiled and linked to form independently saved executable files and then executed, while Java programs are interpreted and executed by the Java virtual machine after compilation.
(4) Script language programs generally exist in the form of text.
(5) Scripts are generally not universal and can handle a limited range of problems.
At present, many scripting languages are widely used, such as Python, ASP, PHP, CGI, JSP, Lua, Tc1, JavaScript, VBScript, Installshield Script, ActionScript, etc. The following is a brief introduction to JavaScript.
2. Writing and execution of JavaScript code JavaScript is a scripting language based on object and event drive and with security performance. It adds executable content to the web page, making the web page break away from the static HTML text form and become a vivid interface that can interact with users. JavaScript is divided into two parts, the server side and the client side, both of which contain the core content of JavaScript. The event based programming mode is used to make the browser generate an event when the document or some of its elements produce certain actions. For example, when the browser carries a page or the mouse moves on the hyperlink text in the web page, Will fire the corresponding event.
(1) JavaScript object
The JavaScript language is based on objects, that is, the scope of JavaScript can be divided into large and small objects, and then continue to divide objects until very detailed. It should be noted that there is a conceptual difference between the so-called "object" here and the object in object-oriented languages (such as C++, Java, etc.).
The object can be a text, a picture, a form, etc. Each object has its own properties, methods and events. The properties of an object reflect some specific properties of the object, such as the length of a string, the length and width of an image, and the text in a textbox. The method of the object can do some things on the object, such as "Submit" of the form, "Scrolling" of the window, and so on. The event of an object can respond to the event that occurs on the object. For example, submitting a form will generate a "submit event" for the form, and clicking a link will generate a "click event" for the form.
There are three types of objects in JavaScript: JavaScript built-in objects, objects provided by browsers, and user-defined objects.
The internal objects of JavaScript include Math, Date, String, Array, Number, Boolean, Function, Global, Object, RegExp and Event. The objects provided by the browser refer to some objects provided by the browser for JavaScript according to the system configuration and the currently loaded page, such as document and window.
User defined objects are new objects defined by users according to their own needs. New objects defined through programming can make JavaScript have more perfect functions, and make the page more personalized and rich.
The way to reference any "property" of an object is "object name. property name". For example, Date is a date object in JavaScript. This object can store any date value, from 0001 to 9999, and can be accurate to milliseconds. Internally, the date object is an integer, which is the number of milliseconds from the zero hour hour on January 1, 1970 to the date the date object currently refers to. If the date is earlier than 1970, it is a negative number. If you do not specify a time zone, all dates and times use the UTC (Universal Time) time zone, which is the same as GMT (Greenwich Mean Time) in value. The following code defines a date object d and takes the current time as its initialization value:
var d=new Date;
If you want to customize the initial value, you can use the following methods: Var d=new Date (2010, 10, 1)://October 1, 2010 perhaps var d=new Date('Oct 1, 2010’);// October 1, 2010
(2) The Writing and Running Environment of JavaScript Programs
Writing JavaScript programs does not require a special software environment, just a text editor and a browser that supports JavaScript, that is, input JavaScript code in the text editor, save it in a file of the corresponding format, and double-click to see the running results in the browser.
(3) JavaScript Program Functions
JavaScript programs can control the content displayed in the web page, control the actions of the browser, interact with HTML forms, interact with users, read and write user status with cookies, and other purposes. The following is a brief description of controlling the content displayed on the web page and interacting with users as an example.
① Control what is displayed in a web page
The following program 2 is a simple example of adding JavaScript script to a Web document. Save the code of program 2 as an html document (such as example. html), open the document with a browser, and you can see the "Hello, World!" displayed on the screen, as shown in Figure 7-9 ② Interact with users
JavaScript is usually used with browsers. The window of each browser and the frame in the window are represented by the window object. The window object provides many methods to interact with users. The simplest and fastest method is dialog operation.
The common methods to interact with users are alert(), confirm(), and prompt(). Their functions are to pop up a simple dialog box. alert() to notify the user of prompt information. confirm() requires the user to confirm a situation, and prompt() is used to enter a string. The format for using the alert() method is:
window.alert(str);
The window can be omitted, and the str indicates the text to be displayed in the message dialog box, enclosed in double quotation marks.
Source Statement: This article is original or edited by Shangpin China's editors. If it needs to be reproduced, please indicate that it is from Shangpin China. The above contents (including pictures and words) are from the Internet. If there is any infringement, please contact us in time (010-60259772).