0% found this document useful (0 votes)
1 views11 pages

JavaScript Where To

The document provides an overview of how to include JavaScript in HTML using the <script> tag, which can be placed in the <head> or <body> sections. It discusses the use of external JavaScript files, their advantages, and how to reference them in HTML. Additionally, it highlights the importance of placing scripts at the bottom of the <body> for improved display speed.

Uploaded by

josephsani730
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views11 pages

JavaScript Where To

The document provides an overview of how to include JavaScript in HTML using the <script> tag, which can be placed in the <head> or <body> sections. It discusses the use of external JavaScript files, their advantages, and how to reference them in HTML. Additionally, it highlights the importance of placing scripts at the bottom of the <body> for improved display speed.

Uploaded by

josephsani730
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

10/11/25, 6:49 AM JavaScript Where To

 Tutorials  References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

JavaScript Where To
❮ Previous Next ❯

The <script> Tag


In HTML, JavaScript code is inserted between <script> and </script> tags.

Example
<script>
[Link]("demo").innerHTML = "My First JavaScript";
</script>

Try it Yourself »

Old JavaScript examples may use a type attribute: <script type="text/javascript">.


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.

[Link] 1/11
10/11/25, 6:49 AM JavaScript Where To

For example, a function can be called when an event occurs, like when the user clicks a
 button.
Tutorials  References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

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

JavaScript in <head> or <body>


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

Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

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

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

Example

<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
[Link]("demo").innerHTML = "Paragraph changed.";
}
</script>
</head>
<body>

<h2>Demo JavaScript in Head</h2>

<p id="demo">A Paragraph</p>


<button type="button" onclick="myFunction()">Try it</button>

</body>
</html>

[Link] 2/11
10/11/25, 6:49 AM JavaScript Where To

Try it Yourself »
Tutorials  References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

ADVERTISEMENT

REMOVE ADS

 

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

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

Example

<!DOCTYPE html>
<html>
<body>

<h2>Demo JavaScript in Body</h2>

<p id="demo">A Paragraph</p>

<button type="button" onclick="myFunction()">Try it</button>

<script>
function myFunction() {
[Link]("demo").innerHTML = "Paragraph changed.";
}
</script>

</body>
</html>

[Link] 3/11
10/11/25, 6:49 AM JavaScript Where To

Try it Yourself »
Tutorials  References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

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

External JavaScript
Scripts can also be placed in external files:

External file: [Link]


function myFunction() {
[Link]("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
<script> tag:

Example
<script src="[Link]"></script>

Try it Yourself »

You can place an external script reference in <head> or <body> as you like.

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

[Link] 4/11
10/11/25, 6:49 AM JavaScript Where To

External scripts cannot contain <script> tags.


 Tutorials  References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

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
<script src="[Link]"></script>
<script src="[Link]"></script>

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 [Link]:

Example
<script src="[Link]

Try it Yourself »

This example uses a file path to link to [Link]:

[Link] 5/11
10/11/25, 6:49 AM JavaScript Where To

 Tutorials 
Example
References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
<script src="/js/[Link]"></script>

Try it Yourself »

This example uses no path to link to [Link]:

Example
<script src="[Link]"></script>

Try it Yourself »

You can read more about file paths in the chapter HTML File Paths.

Video: JavaScript Where to?

[Link] 6/11
10/11/25, 6:49 AM JavaScript Where To

 Tutorials  References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

❮ Previous Next ❯

ADVERTISEMENT

[Link] 7/11
10/11/25, 6:49 AM JavaScript Where To

 Tutorials  References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

COLOR PICKER

[Link] 8/11
10/11/25, 6:49 AM JavaScript Where To

 Tutorials  References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

 

ADVERTISEMENT

REMOVE ADS

ADVERTISEMENT

ADVERTISEMENT

[Link] 9/11
10/11/25, 6:49 AM JavaScript Where To

 Tutorials  References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C


 PLUS SPACES

GET CERTIFIED FOR TEACHERS

FOR BUSINESS CONTACT US

Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
[Link] Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial

Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
[Link] Reference
Bootstrap Reference
PHP Reference
HTML Colors
[Link] 10/11
10/11/25, 6:49 AM JavaScript Where To
Java Reference

 Tutorials  AngularJS Reference


References 
jQuery Reference
Exercises  Get Certified

HTML
 CSS
Top Examples
JAVASCRIPT SQL PYTHON JAVA PHP
Get HOW
Certified
TO [Link] C
HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
[Link] Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full
correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and
privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by [Link].

[Link] 11/11

You might also like