SlideShare a Scribd company logo
Web Application Design
2023-2024
© Adil Chekati 1
-Lecture 2-
Chapter 1 – HTML, CSS, Javascript web technologies
Adil CHEKATI, PhD
adil.chekati@univ-constantine2.dz
Part III: JavaScript language - Overview
Dr. CHEKATI A.
Web Application Design {CAW}
2
Chapter 1 – HTML, CSS, Javascript web technologies
Prerequisites
❏ Basic Understanding of Web Technologies.
❏ Programming Fundamentals
JavaScript
language -
Overview
3
Part III
Objectives
➔ Comprehend the fundamental role and
functionalities of JavaScript as a
programming language for web
applications.
➔ Utilize JavaScript for basic scripting
tasks and interactivity within a web
page.
© Adil Chekati
1. JavaScript basics
Dr. CHEKATI A.
Web Application Design {CAW}
A comprehensive tutorial on web
technologies (HTML, CSS and JavaScript) can
be found at:
https://developer.mozilla.org/fr/docs/Web.
To see all the details of the JavaScript
language with examples and detailed
descriptions of all concepts.
4
Chapter 1 – HTML, CSS, Javascript web technologies
(Please refer to this link for details omitted from this course).
1.1 Location of JavaScript code
Dr. CHEKATI A.
Web Application Design {CAW}
5
Chapter 1 – HTML, CSS, Javascript web technologies
In principle, anywhere in an HTML document, but there are some well-known patterns:
It's advisable to place the
JavaScript code just before
the end tag of the document
body (</body>) to speed up
page display.
1.1 Location of JavaScript code
Dr. CHEKATI A.
Web Application Design {CAW}
6
Chapter 1 – HTML, CSS, Javascript web technologies
JavaScript code can either be located :
In the HTML file of the web page
In an external file
In the file mycode.js:
1.2 JavaScript dialogs
Dr. CHEKATI A.
Web Application Design {CAW}
7
Chapter 1 – HTML, CSS, Javascript web technologies
3 types :
alert() :
to display a message .
Ex : alert("Welcome to my page!")
Display :
confirm():
to display a message with a decision.
Ex :
1.2 JavaScript dialogs
Dr. CHEKATI A.
Web Application Design {CAW}
8
Chapter 1 – HTML, CSS, Javascript web technologies
prompt():
to display a message and receive data from the user
Ex:
3 types :
1.2 JavaScript dialogs
Dr. CHEKATI A.
Web Application Design {CAW}
9
Chapter 1 – HTML, CSS, Javascript web technologies
Example:
1.2 JavaScript dialogs
Dr. CHEKATI A.
Web Application Design {CAW}
10
Chapter 1 – HTML, CSS, Javascript web technologies
JavaScript code can either be located :
1.3 Events in JavaScript
Dr. CHEKATI A.
Web Application Design {CAW}
11
Chapter 1 – HTML, CSS, Javascript web technologies
An event indicates the occurrence of something important that needs to be dealt with.
For example: clicking on something, moving the mouse, pressing a key on the keyboard or a
button...
Code can be scheduled to run when an event occurs, This is called event processing.
Examples of events: onclick, onmouseover, onmouseout, onmouseup, onmousedown,
keypressed etc.
Example: the web page loading event: onload
1.4 Functions in JavaScript
Dr. CHEKATI A.
Web Application Design {CAW}
12
Chapter 1 – HTML, CSS, Javascript web technologies
As in other programming languages, a function is a set of instructions executed when the
function is called. It may or may not have parameters, return a result, make recursive calls
to a function and so on.
➔ In JavaScript, functions play a very important role. Almost all JavaScript code is
written in the form of functions.
➔ A function that returns a result does so through the return keyword.
➔ The return keyword stops the execution of the function.
➔ A function can have another function as a parameter. known as a callBack function.
1.4 Functions in JavaScript
Dr. CHEKATI A.
Web Application Design {CAW}
13
Chapter 1 – HTML, CSS, Javascript web technologies
➔ A function can be declared in 2 ways in JS according to the following examples:
1.4 Functions in JavaScript
Dr. CHEKATI A.
Web Application Design {CAW}
14
Chapter 1 – HTML, CSS, Javascript web technologies
Example:
1.4 Functions in JavaScript
Dr. CHEKATI A.
Web Application Design {CAW}
15
Chapter 1 – HTML, CSS, Javascript web technologies
Example:
> 18 < 18
1.5 Local or global variables
Dr. CHEKATI A.
Web Application Design {CAW}
16
Chapter 1 – HTML, CSS, Javascript web technologies
Variables do not have to be declared (using the var keyword) in JavaScript.
But it is advisable to declare them in their functions to avoid certain errors.
➔ Variables declared inside a function are called local, and can only be used can only be
used within that function
➔ Variables declared in the main program are called global and can be used both used
both inside and outside functions.
➔ If two variables (one global and one local) share the same name, priority is given to
the local variable within the function. for the local variable within its function.
1.6 Objects in JavaScript
Dr. CHEKATI A.
Web Application Design {CAW}
17
Chapter 1 – HTML, CSS, Javascript web technologies
In JavaScript, everything is an object, including basic types and functions.
An object is defined as a set of pairs (attribute : value).
An object can have properties (attributes) and can also have methods.
An object's properties and methods can be accessed using 2
possible notations:
Dot notation person.name
Bracket notation person["name"]
Dot notation requires that the property name does not begin with
a number.
1.7 JavaScript language elements
Dr. CHEKATI A.
Web Application Design {CAW}
18
Chapter 1 – HTML, CSS, Javascript web technologies
Variables do not have to be declared (using the var keyword) in JavaScript.
But it is advisable to declare them in their functions to avoid certain errors.
➔ Functions on arrays
sort(); indexOf(); slice(); reverse() lastIndexOf(); splice().
➔ Iterators
forEach() ; map()
1.8 JavaScript displays
Dr. CHEKATI A.
Web Application Design {CAW}
19
Chapter 1 – HTML, CSS, Javascript web technologies
There are 4 ways of displaying a message in JavaScript:
➔ Dialog boxes: (alert, confirm, prompt)
➔ Writing to the HTML document using document.write(message), which erases
all page content and displays the message only.
➔ Write to an HTML element using innerHTML(possible with DOM)
➔ Write to the browser console using console.log(message) which will will
display the message in the console and not on the web page.
1.9 Error handling and debugging in JavaScript
Dr. CHEKATI A.
Web Application Design {CAW}
20
Chapter 1 – HTML, CSS, Javascript web technologies
JavaScript is a weakly typed language, and is considered one of the most flexible and
easiest to learn. However, it has one major drawback: the occurrence of errors.
➔ In the case of syntactic errors, they are relatively easy to correct.
➔ As for logical errors, they are often undetectable and it's up to the programmer to
find and correct them. To help the programmer in this difficult task, a debugger for
JavaScript is available on the browser (to be seen in Lab).
➔ Verification messages can be displayed in the code not by means of dialog boxes
dialogs (alert, confirm, prompt) but by console.log (message), which
will display the the message in the console and not on the web page (to facilitate
verification by the programmer)
2. DOM:Document Object Model
Dr. CHEKATI A.
Web Application Design {CAW}
21
Chapter 1 – HTML, CSS, Javascript web technologies
A page loaded into a browser is stored as a tree structure called DOM.
The DOM represents an interface between JavaScript and (HTML+CSS), enabling them to
be manipulated by JS programs.
The browser transforms each HTML element into a JavaScript object that can be can
manipulate (modify/delete, etc.).
2. DOM:Document Object Model
Dr. CHEKATI A.
Web Application Design {CAW}
22
Chapter 1 – HTML, CSS, Javascript web technologies
Example:
2.1 Relationships between nodes
Dr. CHEKATI A.
Web Application Design {CAW}
23
Chapter 1 – HTML, CSS, Javascript web technologies
The usefulness of the DOM lies in the fact that it allows you to access the various nodes
representing the structure of a web page, thanks to the relationships that exist between
nodes and the functions used to access them.
In particular, we have the following functions:
➔ Manipulation of parent node: parentNode
➔ Handling child nodes: childNodes[ ], firstChild, lastChild
➔ Handling of sibling nodes: previousSibling, nextSibling
2.1 Relationships between nodes
Dr. CHEKATI A.
Web Application Design {CAW}
24
Chapter 1 – HTML, CSS, Javascript web technologies
Arrangement of nodes and their descendants allows functions to be used as follows:
Using functions, you can find
the path to a node, locate a
node or a set of nodes nodes
in order to apply a certain
process.
2.2 Node selection
Dr. CHEKATI A.
Web Application Design {CAW}
25
Chapter 1 – HTML, CSS, Javascript web technologies
Using the DOM and the preceding functions, you can add, delete, copy or change any node
in the DOM. change any node in the DOM. To do this, we use mechanisms for accessing and
selecting using one of the following 3 methods:
➔ Method 1: Use of the exact path: requires exact knowledge of the there is a risk of
error, as the DOM may differ from one browser to another.
➔ Method 2: Use the element type (the type of its tag type):
getElementsByTagName( ), which requires exact knowledge of the tag of the
element you're looking for (h2 or h3??), with the risk of several elements having that
name.
➔ Method 3: Using the name of the element itself: getElementById( ). This
method is easiest if the element you're looking for has an identifier as follows:
<element_name id="stuff">...</element_name>
2.2 Node selection
Dr. CHEKATI A.
Web Application Design {CAW}
26
Chapter 1 – HTML, CSS, Javascript web technologies
➔ Method 1:
function change_color1(){
document.childNodes[1].childNodes[2]
.childNodes[1].style.color="red";}
➔ Method 2:
function change_color2(){
document.getElementsByTagName("h2")[0
].style.color="yellow"; }
➔ Method 3:
function change_color3(){
document.getElementById("pur_texte"
).style.color="blue"; }
2.2 DOM selectors
Dr. CHEKATI A.
Web Application Design {CAW}
27
Chapter 1 – HTML, CSS, Javascript web technologies
The root of the HTML document is an object called a document, to which a series of
methods can be applied to select particular elements of the document.
In particular, we'll be looking at the following methods:
➔ document.getElementById("id_element"): returns the element with the value
id="id_element"
➔ document.getElementsByClassName("nom_class"): returns a list of elements with the
class class is class_name.
➔ document.getElementsByTagName("nom_tag"): returns a list of elements with the tag as
tag <tag_name>.
➔ document.querySelector("selector_CSS"): can replace all previous methods by
specifying a methods by specifying an id, class or tag name in CSS selector notation. It returns the
first element satisfying the selecteur_CSS selector
➔ document.querySelectorAll("selector_CSS"): works like the previous method but
returns an array of all elements satisfying the selecteur_CSS selector, accessible via indices.
2.2 DOM selectors
Dr. CHEKATI A.
Web Application Design {CAW}
28
Chapter 1 – HTML, CSS, Javascript web technologies
var tag = document.getElementById("highlight")
tag = document.querySelector(”#highlight”)
It returns: <li id="highlight">List Item 1</li>
var tags = document.getElementsByClassName("bolded")
tag = document.querySelectorAll(”.bolded”)
It returns: <li class="bolded">List Item 2</li> et <li
class="bolded">List Item 3</li>
var tags = document.getElementsByTagName("li")
tag = document.querySelectorAll(”li”);
It returns:
<li id="highlight">List Item 1</li>
<li class="bolded">List Item 2</li>
<li class="bolded">List Item 3</li>
2.3 DOM-related events
Dr. CHEKATI A.
Web Application Design {CAW}
29
Chapter 1 – HTML, CSS, Javascript web technologies
Make pages interactive. There are many events that can be applied to DOM elements: click
a button, move to a link, copy and paste, press return, etc.
To support an event, we need to:
➔ Select the element
➔ Place an event listener on it, which must perform processing in response to the event.
Syntax:
To add a listener to an element, use a method called addEventListener()
element.addEventListener(type, functionToCall());
2.3 DOM-related events
Dr. CHEKATI A.
Web Application Design {CAW}
30
Chapter 1 – HTML, CSS, Javascript web technologies
Lab Exercises Submission Guidelines
Dr. CHEKATI A.
Web Application Design {CAW}
➔ Deadline:
At the end of each Lab session (no later than Saturday at 23:59)
To: adil.chekati@univ-constantine2.dz
➔ File's Name to be submitted:
CAW_Lab%_Gr%_NAMEPair1_NAMEPair2.zip
Example : "CAW_Lab1.part1_Gr1_CHEKATI_BOUZENADA.zip"
31
Textbook
Dr. CHEKATI A.
Web Application Design {CAW}
➔ All academic materials will be available on:
Google Drive.
E-learning platform of Constantine 2 University.
Google Classroom.
32
References
Dr. CHEKATI A.
Web Application Design {CAW}
➔ Book:
Haverbeke, Marijn - Eloquent JavaScript: A Modern Introduction to Programming- (2019)
Online Resource:
Mozilla Developer Network-"JavaScript Guide"
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide)
33
Next Lecture
2023-2024
© Adil Chekati 34
-Lecture 3-
Chapter 2 – Advanced JavaScript concepts
Adil CHEKATI, PhD
adil.chekati@univ-constantine2.dz
Questions,
& comments…
📧 adil.chekati@univ-constatine2.dz
35

More Related Content

PDF
CAW_Lecture1_03102023.pdf11111111111111111111
DOCX
JavaScript
PDF
8.-Javascript-report powerpoint presentation
PDF
Dot Net Fundamentals
PPTX
Javascript for web Programming creating and embedding with html
PDF
react-en.pdf
PDF
5a329780735625624 ch10
PDF
Intro to mobile web application development
CAW_Lecture1_03102023.pdf11111111111111111111
JavaScript
8.-Javascript-report powerpoint presentation
Dot Net Fundamentals
Javascript for web Programming creating and embedding with html
react-en.pdf
5a329780735625624 ch10
Intro to mobile web application development

Similar to CAW_Lecture2_10102023.pdfewwwwwwwwwwwwwww (20)

PDF
JavaScript Interview Questions with Answers
DOCX
Server side programming bt0083
PPTX
document object model in web technologies notes for engineering.pptx
PPT
Rob Tweed :: Ajax and the Impact on Caché and Similar Technologies
DOCX
ASP.NET MVC3 RAD
PPT
Rutgers - FrontPage 98 (Advanced)
PPT
Session vii(java scriptbasics)
PPTX
MCA-202-W4-L1.pptx
PDF
react hook and wesite making structure ppt
ODP
Web 2.0
PDF
Oopp Lab Work
PPT
Silverlight 2 for Developers - TechEd New Zealand 2008
DOCX
Decorating code (Research Paper)
PDF
Basic JavaScript Tutorial
PDF
Google Web Toolkit
PDF
Building richwebapplicationsusingasp
PDF
Java Web Programming on Google Cloud Platform [3/3] : Google Web Toolkit
PDF
Frontend Interview Questions PDF By ScholarHat
PDF
Javascript pdf for beginners easy levell
PPT
Ajax toolkit framework
JavaScript Interview Questions with Answers
Server side programming bt0083
document object model in web technologies notes for engineering.pptx
Rob Tweed :: Ajax and the Impact on Caché and Similar Technologies
ASP.NET MVC3 RAD
Rutgers - FrontPage 98 (Advanced)
Session vii(java scriptbasics)
MCA-202-W4-L1.pptx
react hook and wesite making structure ppt
Web 2.0
Oopp Lab Work
Silverlight 2 for Developers - TechEd New Zealand 2008
Decorating code (Research Paper)
Basic JavaScript Tutorial
Google Web Toolkit
Building richwebapplicationsusingasp
Java Web Programming on Google Cloud Platform [3/3] : Google Web Toolkit
Frontend Interview Questions PDF By ScholarHat
Javascript pdf for beginners easy levell
Ajax toolkit framework
Ad

Recently uploaded (20)

PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Cell Types and Its function , kingdom of life
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Pharma ospi slides which help in ospi learning
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Cell Structure & Organelles in detailed.
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
RMMM.pdf make it easy to upload and study
PPTX
Lesson notes of climatology university.
PDF
Classroom Observation Tools for Teachers
PPTX
Institutional Correction lecture only . . .
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Supply Chain Operations Speaking Notes -ICLT Program
Cell Types and Its function , kingdom of life
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Pharma ospi slides which help in ospi learning
TR - Agricultural Crops Production NC III.pdf
Microbial disease of the cardiovascular and lymphatic systems
Renaissance Architecture: A Journey from Faith to Humanism
Cell Structure & Organelles in detailed.
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
RMMM.pdf make it easy to upload and study
Lesson notes of climatology university.
Classroom Observation Tools for Teachers
Institutional Correction lecture only . . .
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Ad

CAW_Lecture2_10102023.pdfewwwwwwwwwwwwwww

  • 1. Web Application Design 2023-2024 © Adil Chekati 1 -Lecture 2- Chapter 1 – HTML, CSS, Javascript web technologies Adil CHEKATI, PhD [email protected] Part III: JavaScript language - Overview
  • 2. Dr. CHEKATI A. Web Application Design {CAW} 2 Chapter 1 – HTML, CSS, Javascript web technologies Prerequisites ❏ Basic Understanding of Web Technologies. ❏ Programming Fundamentals
  • 3. JavaScript language - Overview 3 Part III Objectives ➔ Comprehend the fundamental role and functionalities of JavaScript as a programming language for web applications. ➔ Utilize JavaScript for basic scripting tasks and interactivity within a web page. © Adil Chekati
  • 4. 1. JavaScript basics Dr. CHEKATI A. Web Application Design {CAW} A comprehensive tutorial on web technologies (HTML, CSS and JavaScript) can be found at: https://developer.mozilla.org/fr/docs/Web. To see all the details of the JavaScript language with examples and detailed descriptions of all concepts. 4 Chapter 1 – HTML, CSS, Javascript web technologies (Please refer to this link for details omitted from this course).
  • 5. 1.1 Location of JavaScript code Dr. CHEKATI A. Web Application Design {CAW} 5 Chapter 1 – HTML, CSS, Javascript web technologies In principle, anywhere in an HTML document, but there are some well-known patterns: It's advisable to place the JavaScript code just before the end tag of the document body (</body>) to speed up page display.
  • 6. 1.1 Location of JavaScript code Dr. CHEKATI A. Web Application Design {CAW} 6 Chapter 1 – HTML, CSS, Javascript web technologies JavaScript code can either be located : In the HTML file of the web page In an external file In the file mycode.js:
  • 7. 1.2 JavaScript dialogs Dr. CHEKATI A. Web Application Design {CAW} 7 Chapter 1 – HTML, CSS, Javascript web technologies 3 types : alert() : to display a message . Ex : alert("Welcome to my page!") Display : confirm(): to display a message with a decision. Ex :
  • 8. 1.2 JavaScript dialogs Dr. CHEKATI A. Web Application Design {CAW} 8 Chapter 1 – HTML, CSS, Javascript web technologies prompt(): to display a message and receive data from the user Ex: 3 types :
  • 9. 1.2 JavaScript dialogs Dr. CHEKATI A. Web Application Design {CAW} 9 Chapter 1 – HTML, CSS, Javascript web technologies Example:
  • 10. 1.2 JavaScript dialogs Dr. CHEKATI A. Web Application Design {CAW} 10 Chapter 1 – HTML, CSS, Javascript web technologies JavaScript code can either be located :
  • 11. 1.3 Events in JavaScript Dr. CHEKATI A. Web Application Design {CAW} 11 Chapter 1 – HTML, CSS, Javascript web technologies An event indicates the occurrence of something important that needs to be dealt with. For example: clicking on something, moving the mouse, pressing a key on the keyboard or a button... Code can be scheduled to run when an event occurs, This is called event processing. Examples of events: onclick, onmouseover, onmouseout, onmouseup, onmousedown, keypressed etc. Example: the web page loading event: onload
  • 12. 1.4 Functions in JavaScript Dr. CHEKATI A. Web Application Design {CAW} 12 Chapter 1 – HTML, CSS, Javascript web technologies As in other programming languages, a function is a set of instructions executed when the function is called. It may or may not have parameters, return a result, make recursive calls to a function and so on. ➔ In JavaScript, functions play a very important role. Almost all JavaScript code is written in the form of functions. ➔ A function that returns a result does so through the return keyword. ➔ The return keyword stops the execution of the function. ➔ A function can have another function as a parameter. known as a callBack function.
  • 13. 1.4 Functions in JavaScript Dr. CHEKATI A. Web Application Design {CAW} 13 Chapter 1 – HTML, CSS, Javascript web technologies ➔ A function can be declared in 2 ways in JS according to the following examples:
  • 14. 1.4 Functions in JavaScript Dr. CHEKATI A. Web Application Design {CAW} 14 Chapter 1 – HTML, CSS, Javascript web technologies Example:
  • 15. 1.4 Functions in JavaScript Dr. CHEKATI A. Web Application Design {CAW} 15 Chapter 1 – HTML, CSS, Javascript web technologies Example: > 18 < 18
  • 16. 1.5 Local or global variables Dr. CHEKATI A. Web Application Design {CAW} 16 Chapter 1 – HTML, CSS, Javascript web technologies Variables do not have to be declared (using the var keyword) in JavaScript. But it is advisable to declare them in their functions to avoid certain errors. ➔ Variables declared inside a function are called local, and can only be used can only be used within that function ➔ Variables declared in the main program are called global and can be used both used both inside and outside functions. ➔ If two variables (one global and one local) share the same name, priority is given to the local variable within the function. for the local variable within its function.
  • 17. 1.6 Objects in JavaScript Dr. CHEKATI A. Web Application Design {CAW} 17 Chapter 1 – HTML, CSS, Javascript web technologies In JavaScript, everything is an object, including basic types and functions. An object is defined as a set of pairs (attribute : value). An object can have properties (attributes) and can also have methods. An object's properties and methods can be accessed using 2 possible notations: Dot notation person.name Bracket notation person["name"] Dot notation requires that the property name does not begin with a number.
  • 18. 1.7 JavaScript language elements Dr. CHEKATI A. Web Application Design {CAW} 18 Chapter 1 – HTML, CSS, Javascript web technologies Variables do not have to be declared (using the var keyword) in JavaScript. But it is advisable to declare them in their functions to avoid certain errors. ➔ Functions on arrays sort(); indexOf(); slice(); reverse() lastIndexOf(); splice(). ➔ Iterators forEach() ; map()
  • 19. 1.8 JavaScript displays Dr. CHEKATI A. Web Application Design {CAW} 19 Chapter 1 – HTML, CSS, Javascript web technologies There are 4 ways of displaying a message in JavaScript: ➔ Dialog boxes: (alert, confirm, prompt) ➔ Writing to the HTML document using document.write(message), which erases all page content and displays the message only. ➔ Write to an HTML element using innerHTML(possible with DOM) ➔ Write to the browser console using console.log(message) which will will display the message in the console and not on the web page.
  • 20. 1.9 Error handling and debugging in JavaScript Dr. CHEKATI A. Web Application Design {CAW} 20 Chapter 1 – HTML, CSS, Javascript web technologies JavaScript is a weakly typed language, and is considered one of the most flexible and easiest to learn. However, it has one major drawback: the occurrence of errors. ➔ In the case of syntactic errors, they are relatively easy to correct. ➔ As for logical errors, they are often undetectable and it's up to the programmer to find and correct them. To help the programmer in this difficult task, a debugger for JavaScript is available on the browser (to be seen in Lab). ➔ Verification messages can be displayed in the code not by means of dialog boxes dialogs (alert, confirm, prompt) but by console.log (message), which will display the the message in the console and not on the web page (to facilitate verification by the programmer)
  • 21. 2. DOM:Document Object Model Dr. CHEKATI A. Web Application Design {CAW} 21 Chapter 1 – HTML, CSS, Javascript web technologies A page loaded into a browser is stored as a tree structure called DOM. The DOM represents an interface between JavaScript and (HTML+CSS), enabling them to be manipulated by JS programs. The browser transforms each HTML element into a JavaScript object that can be can manipulate (modify/delete, etc.).
  • 22. 2. DOM:Document Object Model Dr. CHEKATI A. Web Application Design {CAW} 22 Chapter 1 – HTML, CSS, Javascript web technologies Example:
  • 23. 2.1 Relationships between nodes Dr. CHEKATI A. Web Application Design {CAW} 23 Chapter 1 – HTML, CSS, Javascript web technologies The usefulness of the DOM lies in the fact that it allows you to access the various nodes representing the structure of a web page, thanks to the relationships that exist between nodes and the functions used to access them. In particular, we have the following functions: ➔ Manipulation of parent node: parentNode ➔ Handling child nodes: childNodes[ ], firstChild, lastChild ➔ Handling of sibling nodes: previousSibling, nextSibling
  • 24. 2.1 Relationships between nodes Dr. CHEKATI A. Web Application Design {CAW} 24 Chapter 1 – HTML, CSS, Javascript web technologies Arrangement of nodes and their descendants allows functions to be used as follows: Using functions, you can find the path to a node, locate a node or a set of nodes nodes in order to apply a certain process.
  • 25. 2.2 Node selection Dr. CHEKATI A. Web Application Design {CAW} 25 Chapter 1 – HTML, CSS, Javascript web technologies Using the DOM and the preceding functions, you can add, delete, copy or change any node in the DOM. change any node in the DOM. To do this, we use mechanisms for accessing and selecting using one of the following 3 methods: ➔ Method 1: Use of the exact path: requires exact knowledge of the there is a risk of error, as the DOM may differ from one browser to another. ➔ Method 2: Use the element type (the type of its tag type): getElementsByTagName( ), which requires exact knowledge of the tag of the element you're looking for (h2 or h3??), with the risk of several elements having that name. ➔ Method 3: Using the name of the element itself: getElementById( ). This method is easiest if the element you're looking for has an identifier as follows: <element_name id="stuff">...</element_name>
  • 26. 2.2 Node selection Dr. CHEKATI A. Web Application Design {CAW} 26 Chapter 1 – HTML, CSS, Javascript web technologies ➔ Method 1: function change_color1(){ document.childNodes[1].childNodes[2] .childNodes[1].style.color="red";} ➔ Method 2: function change_color2(){ document.getElementsByTagName("h2")[0 ].style.color="yellow"; } ➔ Method 3: function change_color3(){ document.getElementById("pur_texte" ).style.color="blue"; }
  • 27. 2.2 DOM selectors Dr. CHEKATI A. Web Application Design {CAW} 27 Chapter 1 – HTML, CSS, Javascript web technologies The root of the HTML document is an object called a document, to which a series of methods can be applied to select particular elements of the document. In particular, we'll be looking at the following methods: ➔ document.getElementById("id_element"): returns the element with the value id="id_element" ➔ document.getElementsByClassName("nom_class"): returns a list of elements with the class class is class_name. ➔ document.getElementsByTagName("nom_tag"): returns a list of elements with the tag as tag <tag_name>. ➔ document.querySelector("selector_CSS"): can replace all previous methods by specifying a methods by specifying an id, class or tag name in CSS selector notation. It returns the first element satisfying the selecteur_CSS selector ➔ document.querySelectorAll("selector_CSS"): works like the previous method but returns an array of all elements satisfying the selecteur_CSS selector, accessible via indices.
  • 28. 2.2 DOM selectors Dr. CHEKATI A. Web Application Design {CAW} 28 Chapter 1 – HTML, CSS, Javascript web technologies var tag = document.getElementById("highlight") tag = document.querySelector(”#highlight”) It returns: <li id="highlight">List Item 1</li> var tags = document.getElementsByClassName("bolded") tag = document.querySelectorAll(”.bolded”) It returns: <li class="bolded">List Item 2</li> et <li class="bolded">List Item 3</li> var tags = document.getElementsByTagName("li") tag = document.querySelectorAll(”li”); It returns: <li id="highlight">List Item 1</li> <li class="bolded">List Item 2</li> <li class="bolded">List Item 3</li>
  • 29. 2.3 DOM-related events Dr. CHEKATI A. Web Application Design {CAW} 29 Chapter 1 – HTML, CSS, Javascript web technologies Make pages interactive. There are many events that can be applied to DOM elements: click a button, move to a link, copy and paste, press return, etc. To support an event, we need to: ➔ Select the element ➔ Place an event listener on it, which must perform processing in response to the event. Syntax: To add a listener to an element, use a method called addEventListener() element.addEventListener(type, functionToCall());
  • 30. 2.3 DOM-related events Dr. CHEKATI A. Web Application Design {CAW} 30 Chapter 1 – HTML, CSS, Javascript web technologies
  • 31. Lab Exercises Submission Guidelines Dr. CHEKATI A. Web Application Design {CAW} ➔ Deadline: At the end of each Lab session (no later than Saturday at 23:59) To: [email protected] ➔ File's Name to be submitted: CAW_Lab%_Gr%_NAMEPair1_NAMEPair2.zip Example : "CAW_Lab1.part1_Gr1_CHEKATI_BOUZENADA.zip" 31
  • 32. Textbook Dr. CHEKATI A. Web Application Design {CAW} ➔ All academic materials will be available on: Google Drive. E-learning platform of Constantine 2 University. Google Classroom. 32
  • 33. References Dr. CHEKATI A. Web Application Design {CAW} ➔ Book: Haverbeke, Marijn - Eloquent JavaScript: A Modern Introduction to Programming- (2019) Online Resource: Mozilla Developer Network-"JavaScript Guide" (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide) 33
  • 34. Next Lecture 2023-2024 © Adil Chekati 34 -Lecture 3- Chapter 2 – Advanced JavaScript concepts Adil CHEKATI, PhD [email protected]