Lecture 01 - HTMLJavaScript (1)
Lecture 01 - HTMLJavaScript (1)
1
Contents
◆ HTML DOM
◆ HTML Styles – CSS
◆ HTML Forms
◆ HTML Events
◆ HTML JavaScript
◆ Web APIs
2
References
◆ HTML: https://www.w3schools.com/html/
◆ CSS: https://www.w3schools.com/css/
◆ JavaScript: https://www.w3schools.com/js/
3
What is DOM?
◆ The Document Object Model (DOM)
DOM represents a document as a tree structure, where
Each node is an object representing a part of the document.
Nodes:
• Document Node: The root node that represents the entire document.
• Element Node: Represents HTML or XML elements.
• Attribute Node: Represents attributes of HTML or XML elements.
• Text Node: Represents the text content inside elements
4
DOM tree
5
HTML Elements
◆ <tagname>Content goes here...</tagname>
6
Accessing DOM Elements (1)
◆ When an HTML document is loaded into a web browser, it becomes
a document object.
◆ The document object is the root node of the HTML document.
◆ The document object is a property of the window object
window.document or document
let url = window.document.URL;
let url = document.URL;
7
Accessing DOM Elements (2)
◆ Using JavaScript
8
What is CSS?
◆ Cascading Style Sheets (CSS) is used to format the layout of a
webpage.
color, font, the size of text, the spacing between elements,…
◆ Cascading
a style applied to a parent element will also apply to all children elements
within the parent
9
Using CSS
◆ Inline - by using the style attribute inside HTML elements
<h1 style="color:blue;">A Blue Heading</h1>
<p style="color:red;">A red paragraph.</p>
10
HTML Forms
◆ An HTML form is used to collect user input.
◆ The user input is most often sent to a server for processing.
11
The <label> Element
◆ The for attribute of the <label> tag should be equal to the id
attribute of the <input> element to bind them together.
12
The form Attribute
◆ An input field located outside of the HTML form but still a part of
the form
13
The formaction Attribute
◆ An HTML form with two submit buttons, with different actions
14
The formmethod Attribute
◆ A form with two submit buttons with "get" and "post“ method
15
HTML DOM Events
◆ DOM Events allow JavaScript to add event listener or event
handlers to HTML elements.
HTML onclick
• <button onclick="myFunction()">Click me</button>
JavaScript click
• button.addEventListener("click", myFunction);
16
HTML JavaScript
◆ Why JavaScript?
HTML to define the content of web pages
CSS to specify the layout of web pages
JavaScript to program the behavior of web pages
◆ Version of JavaScript
The Original JavaScript ES1 ES2 ES3 (1997-1999)
The First Main Revision ES5 (2009)
The Second Revision ES6 (2015)
The Yearly Additions (2016, 2017 ... 2021, 2022)
17
JavaScript Can Change HTML Styles (CSS)
◆ Using the HTML <script> tag to define a client-side script
(JavaScript).
18
JavaScript Functions
◆ JavaScript in <head>
◆ JavaScript in <body>
19
External JavaScript
◆ External file myScript.js
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
◆ External References
<script src="https://www.w3schools.com/js/myScript.js"></script>
20
The Window Object
◆ The window object represents an open window in a browser.
Window alert()
Window confirm()
Window Console Object: access to the browser's debugging console.
window.console.error("You made a mistake");
console.error("You made a mistake");
console.log("Hello World!");
21
Web APIs
◆ API stands for Application Programming Interface.
◆ A Web API is an application programming interface for the Web.
◆ A Browser API can extend the functionality of a web browser.
◆ A Server API can extend the functionality of a web server.
22
Browser APIs
◆ All browsers have a set of built-in Web APIs to support complex
operations, and to help accessing data.
◆ For example, the Geolocation API can return the coordinates of
where the browser is located.
23
Third Party APIs
◆ Third party APIs are not built into your browser.
◆ To use these APIs, you will have to download the code from the
Web.
◆ Examples:
YouTube API - Allows you to display videos on a web site.
Twitter API - Allows you to display Tweets on a web site.
Facebook API - Allows you to display Facebook info on a web site.
24
Exercise
◆ Xây dựng trang html có sử dụng
External CSS
Form và Events
External JavaScript, sử dụng function để thay đổi CSS, thay đổi elements
của form
console.log và console.error
Web APIs (có sẵn trong trình duyệt) hoặc third-party APIs
25