The main way to create dynamic effects on web pages is through the use of client-side scripts. They are pieces of code that are interpreted by the scripting engine of the application displaying the document. JavaScript is a popular programming language used for scripting. In order for the script to be able to execute, it must be inserted into the document in some way.
It is necessary
- - script code;
- - source document;
- - text editor.
Instructions
Step 1
Determine the best way to insert the script. If it is assumed that the same script will be used in different documents, then it makes sense to move it to an external file and connect it from there. Otherwise, it is worth embedding the script text in the body of the page. If the size of the script is large, it is worth placing it in a separate block in the document. If the program code is small in volume and implements a reaction to any event, it is permissible to place it in an attribute that defines an element's built-in event handler or the href attribute of a link.
Step 2
Paste the script from an external source. Add a SCRIPT element with the specified src and type attributes to the document body or HEAD section. For example, you can do this like this: The value of the src attribute must be a URI pointing to the resource from which the script text will be loaded. The charset attribute is optional. It indicates the encoding of the script text. But if it is omitted, the encoding value will be taken as the encoding value of the document into which the script is inserted.
Step 3
Insert the script into the text of the page. Use a SCRIPT element with script text inside. For example, it might look like this:
alert ("The script is working!");
HTML comments that enclose the code are required for compatibility with older browsers. You can use XML comments in XHTML documents.
Step 4
Paste javascript code into inline event handlers. Find the element in the document whose event you want to handle. Add an attribute that defines the appropriate handler. For example, the attribute defining the handler for clicking on an element is onclick, the handler for the loss of focus signal is onblur, and the keystroke handler is onkeydown.
Step 5
Enter the script code as the text that specifies the value of the added attribute. For example, to display a message when a DIV element is clicked with the mouse, you can use the following construction: Text text text Note that to define string literals in the inline element handler code, you must use a different type of quotes than those that specify the value of the HTML attribute …
Step 6
Add the script to the href element of the link. The href value must be a URL, the protocol descriptor of which is "javascript:", and the address portion is a computed expression. For example: Link text If you need to use multiple expressions, use a statement block defined by the brackets {and}.