Using JavaScript By: Franciscus Agnew

Using JavaScript

JavaScript is most commonly used for client-side scripting within an HTML document. As such, this section will focus on how to apply JavaScript in this way.

HTML provides a means of including JavaScript code in the form of the <script> element. Using the <script> element, JavaScript can be included in a web page in two ways:

  • Internal Script
  • External Script

Note: It is no longer necessary to include "type = text/javascript" within the <script> tag as JavaScript is the default scripting language in all modern browsers and in HTML5.

Internal Script

Internal scripts include JavaScript code directly in the markup by using the <script> tag, like this:

This method is a good choice for quickly testing a solution or adding code that is specific to a particular page.

External Script

External scripts instruct the browser to download a separate JavaScript file and execute it in the page. The src attribute points to the script file like any other URL.

The myscript.js file would look like this:

In most cases, this is the best solution for a few reasons:

  • Placing the JavaScript code directly in the HTML file with an internal script makes the HTML files larger and harder to maintain.
  • JavaScript can easily be included in multiple pages just by including the same <script> tag
  • Most browsers cache JavaScript files so they don't have to download a script file twice when visiting another page that uses it.
Next up... Placing JavaScript
Created By
Franciscus Agnew
Appreciate

Made with Adobe Slate

Make your words and images move.

Get Slate

Report Abuse

If you feel that this video content violates the Adobe Terms of Use, you may report this content by filling out this quick form.

To report a Copyright Violation, please follow Section 17 in the Terms of Use.