HTML, CSS and JavaScript basics tutorial

Internal Inclusion of JavaScript – Guidelines for HTML Documents

All videos of the tutorial HTML, CSS and JavaScript basics tutorial
Jan Brinkmann
By Jan Brinkmann Last updated on

JavaScript is an essential technology for modern web development. In this guide, you will learn how to effectively embed JavaScript into an HTML document, particularly through the use of internal JavaScript. This means that the JavaScript code is placed directly in the HTML document. By taking this approach, you gain the ability to integrate dynamic functionality into your website without having to rely on external files. Let's explore the basics of this approach together.

Basics JavaScript - 02 Internal JavaScript

The video is only loaded from YouTube when you play it – data (including your IP address) is then transmitted to YouTube/Google. More in the privacy policy

Step-by-Step Guide

Step 1

Start by creating a new HTML document or opening an existing one. You will use a simple example to demonstrate how to embed JavaScript. Add a <script> tag where you write your JavaScript code.

Internal Inclusion of JavaScript – Guide for HTML Documents

For example, here is a code snippet that creates a counting function. When the user clicks a button, the number increases. This shows how to create and test internal scripts.

Step 2<script>-Tags-Tag instructions the browser to interpret the JavaScript code. You can specify the type of script by using the type attribute. It is recommended to use text/javascript. Here is an example:

Internal Integration of JavaScript – Guide for HTML Documents

Make sure that this tag is located in the -section or just before the closing -tag. Ensure that the JavaScript code is only processed once the webpage has loaded.

Step 3

Inserting JavaScript in the can lead to delays in displaying the webpage. The browser takes time to process the script. This can be frustrating for the user, as they may see just blank pages while the JavaScript is loading. To avoid this, place your scripts better at the end of the HTML document, just before the closing -tag.

Internal Inclusion of JavaScript – Guide for HTML Documents

This not only improves the loading speed of the page but also ensures that the user sees the content quicker. This is particularly relevant when you are using computationally intensive scripts.

Step 4<noscript>-Tags tag. Within this tag, you can provide alternative content or inform the user that JavaScript is required. A simple example could look like this:

Internal Embedding of JavaScript – Guide for HTML Documents

This tag is especially useful to ensure a user-friendly experience even if JavaScript is not active.

Step 5

Internal embedding of JavaScript – Guide for HTML documents

However, keep in mind that many modern websites are now reliant on JavaScript, so the layout and functionality may be significantly limited without activated JavaScript.

Step 6

If you have followed all of the above steps, test the website by loading it in the browser and turning JavaScript on or off. This is helpful to ensure that everything works both with JavaScript enabled and disabled.

Internal embedding of JavaScript – Instructions for HTML documents

Frequently Asked Questions

How do I embed JavaScript in an HTML page?You can place JavaScript directly in the HTML document using the -Tag.

What is the difference between internal and external JavaScript?Internal JavaScript is placed within the HTML document, while external JavaScript is stored in a separate file.

Why should I insert JavaScript at the end of the HTML document?This improves loading times and ensures that the content is displayed faster before the JavaScript is loaded.

What is the -Tag?The -Tag allows the provision of content that is displayed when JavaScript is disabled in the browser.

What is the purpose of the type attribute in the -Tag?It specifies the type of the script, with text/javascript being the common standard.

274
Jan Brinkmann
Trainer About Jan Brinkmann Web development with passion! Jan has been a developer for over 15 years. All courses by Jan Brinkmann