How do i link javascript?

❮ HTML

Try it Yourself »


Definition and Usage

The src attribute specifies the URL of an external script file.

If you want to run the same JavaScript on several pages in a web site, you should create an external JavaScript file, instead of writing the same script over and over again. Save the script file with a .js extension, and then refer to it using the src attribute in the

Note: Point to the external script file exactly where you would have written the script.


Browser Support

Attribute
src Yes Yes Yes Yes Yes

Syntax

Attribute Values

ValueDescription
URL The URL of the external script file.

Possible values:

  • An absolute URL - points to another web site (like src="http://www.example.com/example.js")
  • A relative URL - points to a file within a web site (like src="/scripts/example.js")

❮ HTML

  • Writing the code in your HTML
  • Including it as a link to an external file

For the most part, you will include the JavaScript as an external file.

The Script Tag

The

When using the script tag, we must always use the attribute name and value of type="text/javascript".

Using the script tag to include an external JavaScript file

To include an external JavaScript file, we can use the script tag with the attribute src. You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file.

This script tag should be included between the tags in your HTML document.

JavaScript Files

JavaScript files are not HTML files or CSS files.
  • Always end with the js extension
  • Only include JavaScript

It's customary to put all JavaScript files in a folder called js on websites, like so:

How do i link javascript?

Simple Demo of Including JavaScript

Here's a very simple demonstration of how to include an external JavaScript file into an HTML page.

  • Basic JavaScript Example

Other People's JavaScript

For this class you are not expected to write any actual JavaScript code. Lucky for you, many of people have already written lots of JavaScript and even allow you to use it for free.

JavaScript Frameworks

A Framework is basically a library of code for a certain language. Generally, the framework abstracts common tasks and makes it easier and faster for designers and developers to write their specific code. Frameworks don't really do anything by themselves, they just provide an easier platform for people to build on.

Common JavaScript Frameworks:

  • JQuery
  • Prototype
  • MooTools
  • script.aculo.us

Of these frameworks, JQuery is currently the most popular one. It's also the one you're most like to encounter being used as a buzz word.

JavaScript File Sizes

Many JavaScript files can tend to be rather large, which can slow down the load time of your page. Popular frameworks usually offer a "minified" version of their code. You should always use this version in your pages because it will have a smaller file size.

Back to Class 9 page »

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the tags in your HTML document.
You can use the