How To Connect Java Script

Table of contents:

How To Connect Java Script
How To Connect Java Script

Video: How To Connect Java Script

Video: How To Connect Java Script
Video: 4: How to Include JavaScript in Our HTML | JavaScript Tutorial | Learn JavaScript | For Beginners 2024, May
Anonim

JavaScript is by far the most popular scripting language for web pages and the de facto standard for the web. To date, a huge number of JavaScript scripts have been created, most of which are free and free. Thanks to this, any webmaster can find a script that suits his current needs. And all he will need to know is how to connect the Java script to the web page.

How to connect Java script
How to connect Java script

Necessary

the ability to edit the document code

Instructions

Step 1

Include a script from an external source, addressed by a known URI, to the document. Use the SCRIPT HTML element with the specified src attribute value. Edit the document by adding a construction of the form to it:

Here, the value of script_URI must be a URI that identifies the resource from which the script data will be loaded.

The character encoding of the script should be specified as the value for the charset attribute, if it differs from the character encoding of the document. The encoding of the document is determined based on the data in the Content-Type field of the server's HTTP response header or the value of the content attribute of the META element with the http-equiv attribute set to Content-Type.

Connecting scripts in this way is often done in the header of the document (SCRIPT elements are located inside the HEAD element) and their immediate execution is not required. In this case, it makes sense to use the defer attribute for deferred interpretation of the script text.

Step 2

Include the script by embedding it directly into the document. Add a SCRIPT element to your document markup that has JavaScript content. Use a construction like:

// script code text

// script code text

// script code text

Pay attention to the HTML comments surrounding the script code. They are required to maintain compatibility with older browser models.

Step 3

Include the script by specifying "javascript:" as the protocol descriptor URI of the target anchor of element A. Create a link in the document with the value of the href attribute of the form:

javascript:

Here, the value should be a computed expression in JavaScript. In this case, several sentences of the language can be combined into one expression using operator brackets, for example:

text

When such a link is activated in any way (user or programmatically), the embedded script code will be executed.

Step 4

Add scripts to your document as code for inline element event handlers. To do this, define built-in handlers for the desired events of the selected elements by adding appropriate attributes to these elements. Enter JavaScript snippets for attribute values. For example, you can add code as a handler for the single click event on a DIV element like this:

Element content

For a list of element events that must be supported by conformal user agents, see the Events section of the Level 2 and 3 DOM Specification (DOM2 and DOM3) on the W3C's w3c.org website.

Recommended: