In Internet programming, it is constantly necessary to attract a certain virtual performer to perform the actions necessary for the programmer in the browser or on the server. Actions can be, for example, visual effects or the processing of data entered by the visitor in the browser. Or assembling the requested page from separate blocks on the server. The virtual executor of these actions will be the server or browser software, and the script for the executor will have to be written in one of the scripting programming languages. To get a general idea of how scripts are made, let's write a simple script in JavaScript.
Instructions
Step 1
JavaScript is executed directly in the browser, so everything you need to write and execute is already on your computer. As a working tool for the programmer, we will use a regular text editor - standard Notepad. This is enough for creating a simple script, but of course, for constant programming it is better to use a specialized editor. First step: create a new document in notepad to write instructions to the browser.
Step 2
Now you can start writing the instruction code. The browser understands more than one language - for example, HTML (HyperText Markup Language) is used to markup a page, and Cascading Style Sheets (CSS) is used to provide an extended description of the appearance of page elements. To let the script writer know that this part of the page's source code is written in JavaScript, all instructions must be placed inside the opening and closing tags:
The instructions for the browser are called language operators. For example, the instruction to read and remember the current date and time of the computer for later use in a script looks like this: var aTime = new Date (); Now the aTime object contains date and time data and can be retrieved and processed if necessary. Another instruction - to print some message in the body of the page - looks like this: document.write ("some message"); Pay attention - here an object named "document" is specified, it does not need to be created, this happens automatically. It is a virtual image of the current page. From this object, you can extract information about the page and you can perform various transformations with it - for example, in this line of code, you wrote the text "any message" to the document using the write statement. Now use both of these lines in the script - write the current time on the page: document.write ("Current time" + aTime.getHours () + ":" + aTime.getMinutes ()); Here, with a simple addition (+), you concatenate the four parts of the printable string. When finished, your simple script will look like this:
var aTime = new Date ();
document.write ("Current time" + aTime.getHours () + ":" + aTime.getMinutes ());
Step 3
Final step: save the script with html or htm extension (for example timeJS.html). To see what you get, open the file in your browser - just double-click it.