Chapter 5 - Javascript in HTML
Chapter 5 - Javascript in HTML
Chapter 5 - Javascript in HTML
IN HTML
DOCUMENTS
INTRODUCTION
TO JAVASCRIPT
A high-level, dynamic, untyped, and
interpreted programming language.
Used to create interactive web pages,
providing behavior to websites.
It is an essential part of web technologies
alongside HTML and CSS.
Supported by all modern browsers without
the need for plugins.
IMPORTANCE OF
JAVASCRIPT
Why is JavaScript important?
It helps add interactive elements to HTML pages
It is a scripting language which is also a lightweight
programming language,
Everyone can use Javascript without needing a
license.
It it supported by all major browsers.
WRITING A BASIC
JAVASCRIPT
PROGRAM
IN THE <HEAD> TAG
Pros:
Immediate Access: Scripts are available as soon as the page starts
loading.
Control Over Timing: Suitable for scripts that need to run during the
initial page load (e.g., analytics).
Cons:
Render Blocking: Slows down page loading; no content is displayed until
scripts finish executing.
Delayed User Experience: The page may take longer to render and
display visible content.
Best Use:
Use for essential scripts, such as tracking/analytics, and when using
async or defer to prevent blocking.
IN THE <BODY> TAG
Pros:
Faster Page Load: Content loads first, improving perceived performance.
Non-blocking: Scripts don’t delay the rendering of the page content.
Better for Dynamic Content: The DOM is fully loaded, ensuring smooth
interaction with elements.
Cons:
Delayed Script Execution: Scripts only run after the entire HTML is
loaded, which may cause a delay in critical functionalities.
Best Use:
Use for non-essential, interactive scripts that manipulate or interact with
the page content after it’s rendered.
USING AN EXTERNAL
JAVASCRIPT FILE
Separation of Concerns: Keep HTML, CSS, and JavaScript
in separate files for better organization.
Reusability: The same JavaScript file can be linked across
multiple web pages.
Maintainability: Easier to manage code and debug errors.
THE OBJECT MODEL IN
JAVASCRIPT
The JavaScript Object Model refers to how JavaScript
represents and manipulates data as objects. An object
in JavaScript is a collection of key-value pairs, where
each key (or property) has a corresponding value,
which can be data or a function (called a method).
THE OBJECT MODEL IN JAVASCRIPT
THE OBJECT MODEL IN JAVASCRIPT