JS TutorialLesson #3

JavaScript Where To

In HTML, JavaScript code is inserted between and tags.

The Tag

In HTML, JavaScript code is inserted between and tags.

Example

Example Code

html
Try it Yourself
document.getElementById("demo").innerHTML = "My First JavaScript";
Old JavaScript examples may use a type attribute: . > The type attribute is not required. JavaScript is the default scripting language in HTML.

JavaScript Functions and Events

A JavaScript function is a block of JavaScript code, that can be executed when "called" for.

For example, a function can be called when an event occurs, like when the user clicks a button.

You will learn much more about functions and events in later chapters.

JavaScript in or **

You can place any number of scripts in an HTML document.

Scripts can be placed in the , or in the section of an HTML page, or in both.


JavaScript in

In this example, a JavaScript function is placed in the section of an HTML page.

The function is invoked (called) when a button is clicked:

Example

Example Code

html
Try it Yourself
function myFunction() {

  document.getElementById("demo").innerHTML = "Paragraph changed.";

}

## Demo JavaScript in Head

A Paragraph

 Try
it

JavaScript in

In this example, a JavaScript function is placed in the section of an HTML page.

The function is invoked (called) when a button is clicked:

Example

Example Code

html
Try it Yourself
## Demo JavaScript in Body

A Paragraph

   Try
it

function myFunction() {

    document.getElementById("demo").innerHTML = "Paragraph changed.";

}
Placing scripts at the bottom of the element improves the display > speed, because script interpretation slows down the display.

External JavaScript

Scripts can also be placed in external files:

External file: myScript.js

Example Code

javascript
Try it Yourself
function myFunction() {

  document.getElementById("demo").innerHTML = "Paragraph changed.";

}

External scripts are practical when the same code is used in many different web pages.

JavaScript files have the file extension .js.

To use an external script, put the name of the script file in the src (source) attribute of a tag:

Example

Example Code

html
Try it Yourself

You can place an external script reference in or as you like.

The script will behave as if it was located exactly where the tag is located.

External scripts cannot contain tags.

External JavaScript Advantages

Placing scripts in external files has some advantages:

  • It separates HTML and code
  • It makes HTML and JavaScript easier to read and maintain
  • Cached JavaScript files can speed up page loads

To add several script files to one page - use several script tags:

Example

Example Code

html
Try it Yourself

External References

An external script can be referenced in 3 different ways:

  • With a full URL (a full web address)
  • With a file path (like /js/)
  • Without any path

This example uses a full URL to link to myScript.js:

Example

Example Code

html
Try it Yourself

This example uses a file path to link to myScript.js:

Example

Example Code

html
Try it Yourself

This example uses no path to link to myScript.js:

Example

Example Code

html
Try it Yourself
You can read more about file paths in the chapter HTML > File Paths.

Video: JavaScript Where to?


Interactive Code Examples

JS Example 1

html
Try it Yourself
JavaScript syntax and algorithm demonstration.
<script>

document.getElementById("demo").innerHTML = "My First JavaScript";

</script>

JS Example 2

html
Try it Yourself
Interactive runnable JavaScript example from EA Academy.
Live Output Preview:

JS Example 3

html
Try it Yourself
Interactive runnable JavaScript example from EA Academy.
Live Output Preview:

External file: myScript.js

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
function myFunction() {

  document.getElementById("demo").innerHTML = "Paragraph changed.";

}

JS Example 5

html
Try it Yourself
JavaScript syntax and algorithm demonstration.
<script src="myScript.js"></script>

JS Example 6

html
Try it Yourself
JavaScript syntax and algorithm demonstration.
<script src="myScript1.js"></script>

<script src="myScript2.js"></script>