Web Technology · Unit 2 · 10 hrs
Hyper Text Markup Language
Exam-focused notes for Hyper Text Markup Language (Web Technology, CSC329): what the TU syllabus asks and how it has actually been tested, with 11 solved past questions from this unit.
Hyper Text Markup Language in short
HTML gives every web page its structure, the skeleton that CSS later styles and JavaScript later animates, so this unit works through its tags and attributes from the ground up to the semantic and event-handling features HTML5 added.
What this unit covers
- Introduction to HTML
- Elements of HTML Document
- HTML Elements and HTML Attributes
- Headings
- Paragraph
- Division
- Formating: b, i, small, sup, sub
- Spacing: Pre, Br
- Formatting Text Phrases: span, strong, tt
- Image element
- Anchors
- Lists: Ordered and Unordered and Definition
- Tables
- Frames
- Forms: Form Elements, ID attributes, Class Attributes of HTML Elements
- Meta Tag
- Audio
- Video
- Canvas
- Main
- Section
- Article
- Header
- Footer
- Aside
- Nav
- Figure Tags
- HTML Events: Window Events, Form Element Events, Keyboard Events, Mouse Events
Audio
2082 · 5 marks
Write an HTML script using audio tag. Set the autoplay, preload and loop properties to appropriate values. [5]
HTML Events
2082 · 5 marks
Write an HTML script to demonstrate the onkeydown and onkeypress events. [5]
Show solved answerHide answer
HTML Script to Demonstrate onkeydown and onkeypress Events
Brief Explanation
Keyboard Events are:
| Attribute | Description |
|---|---|
onkeydown | Fires/runs when a user is pressing a key |
onkeypress | Fires when user presses a key |
onkeyup | Fires when user releases a key |
These are event handlers written inside HTML tags that execute JavaScript when something happens (such as pressing a key).
HTML Script
<!DOCTYPE html>
<html>
<head>
<title>onkeydown and onkeypress Event Demo</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 40px;
}
input {
padding: 8px;
font-size: 16px;
width: 300px;
}
#output {
margin-top: 20px;
padding: 10px;
background-color: #f0f0f0;
border: 1px solid #ccc;
width: 320px;
}
</style>
</head>
<body>
<h2>Keyboard Event Demo: onkeydown and onkeypress</h2>
<p>Type something in the textbox below:</p>
<input
type="text"
id="myInput"
onkeydown="keyDownHandler(event)"
onkeypress="keyPressHandler(event)"
placeholder="Type here..."
/>
<div id="output">
<p id="keydown_msg">onkeydown: (waiting...)</p>
<p id="keypress_msg">onkeypress: (waiting...)</p>
</div>
<script type="text/javascript">
// Fires when a user is pressing a key (onkeydown)
function keyDownHandler(event) {
document.getElementById("keydown_msg").innerHTML =
"onkeydown: Key pressed down = " + event.key;
}
// Fires when user presses a key (onkeypress)
function keyPressHandler(event) {
document.getElementById("keypress_msg").innerHTML =
"onkeypress: Key pressed = " + event.key;
}
</script>
</body>
</html>
Output / Explanation
When the user types in the text box:
onkeydownfires first, as soon as the key is pushed down, and displays the key name in the output area.onkeypressfires next, when the key press is registered, and also displays the key name.
Example output when user presses the key A:
onkeydown: Key pressed down = A
onkeypress: Key pressed = A
Note:
onkeypressdoes not fire for non-character keys (like Shift, Ctrl, Arrow keys), whereasonkeydownfires for all keys.
2081 · 5 marks
What is onload event in HTML? Write an HTML script that displays alert message 'Hello Nepal' in onload event. [5]
Show solved answerHide answer
Onload Event in HTML
Definition
The onload event is an event handler in HTML/JavaScript that is triggered automatically when a web page (or an element such as an image) has completely finished loading in the browser. It is commonly used with the <body> tag.
- It belongs to the category of Event Handlers -- JavaScript code placed inside HTML tags that execute when something happens.
- The basic syntax of an event handler is:
name_of_handler = "JavaScript code goes here"
For the onload event specifically:
<body onload="JavaScript code or function call">
The onload event is useful for:
- Displaying welcome messages when a page loads
- Initializing variables or functions automatically
- Checking cookies or browser information on page start
HTML Script: Display Alert 'Hello Nepal' on Onload Event
<html>
<head>
<title>Onload Event Example</title>
<script type="text/javascript">
function showMessage() {
alert("Hello Nepal");
}
</script>
</head>
<body onload="showMessage()">
<h2>Welcome to Nepal!</h2>
<p>This page demonstrates the onload event in HTML.</p>
</body>
</html>
Explanation of the Script
| Part | Description |
|---|---|
<script type="text/javascript"> | Defines the JavaScript block inside the <head> tag |
function showMessage() | A JavaScript function that triggers an alert box |
alert("Hello Nepal") | Displays a popup dialog with the message Hello Nepal |
<body onload="showMessage()"> | Calls showMessage() automatically when the page finishes loading |
How It Works
- The browser loads the HTML page completely.
- As soon as the
<body>finishes loading, theonloadevent is fired. - The
showMessage()function is called automatically. - An alert box appears with the message "Hello Nepal".
- The user clicks OK and sees the rest of the page content.
Note: The
onloadevent handler follows the same event handling principle described in the curriculum: event handlers are placed inside HTML tags (not inside<script>tags) and execute JavaScript when a specific event occurs.
2080 · 5 marks
What are HTML keyboard events? Write a script to display a message 'Hello world' on keypress event. [5]
Tables
2081 · 5 marks
Write a HTML script to create a table with three rows and three columns. The second row third column should contain a text 'TU' with link set to www.tu.edu.np. The height of second row should be set to 200px. [5]
Lists
2079 · 5 marks
Write a HTML script to generate following list of items; 1. Coffee : I. Black coffee II. Green coffee 2. Tea A: Black tea B. Green tea [5]
Show solved answerHide answer
HTML Script for Nested Lists
Analysis of Required Output
The question requires:
- Outer ordered list (numbered: 1, 2) for Coffee and Tea
- Inner ordered list under Coffee with Roman numerals (I, II) for Black coffee and Green coffee
- Inner ordered list under Tea with uppercase letters (A, B) for Black tea and Green tea
HTML Code
<!DOCTYPE html>
<html>
<head>
<title>Nested List Example</title>
</head>
<body>
<ol type="1">
<li>Coffee
<ol type="I">
<li>Black coffee</li>
<li>Green coffee</li>
</ol>
</li>
<li>Tea
<ol type="A">
<li>Black tea</li>
<li>Green tea</li>
</ol>
</li>
</ol>
</body>
</html>
Output
1. Coffee
I. Black coffee
II. Green coffee
2. Tea
A. Black tea
B. Green tea
Key Points (from notes)
| Tag / Attribute | Purpose |
|---|---|
<ol> | Ordered list (numbered by default) |
<li> | Each list item |
type="1" | Numeric numbering (1, 2, 3...) |
type="I" | Uppercase Roman numerals (I, II, III...) |
type="A" | Uppercase alphabetic (A, B, C...) |
Note: The
typeattribute in the<ol>tag is used to change the list marker style. Nesting is achieved by placing one<ol>inside a<li>of the outer<ol>.
2078 · 5 marks
Create a HTML page containing ordered and unordered lists. Set the value of ordered list type to 'A'. The list should start at 'D'. [5]
Canvas
2079 · 5 marks
Create a HTML page containing a div having id 'div1'. The div should contain canvas element. The id of canvas should be 'mycanvas'. The height and width of canvas should be 200 and 300. [5]
Image element
2078 · 5 marks
Create an HTML page with its body containing a div. The div should contain an image within it. Create a link on the image to redirect to the url http://www.tuiost.edu.np. Set the title of the page to “iost”. Add Meta tag on the page. [5]
Header
2076 · 5 marks
Create a HTML page with tags header, article and footer. Insert alink containing mail to [email protected] in the footer tag. Set the keywords 'iost', 'csit' using Meta tag in the page. [5]
Frames
2076 · 5 marks
Why inline frames are used? Create a HTML page containing iframe within a paragraph. The iframe have source to http://www.tuiost.edu.np and its height and width are 200px and 400px respectively. [5]
Make Unit 2 stick
Practice CSC329 with flashcards & quizzes