Lesson 8 (E)
Lesson 8 (E)
Lesson 8 (E)
Lesson Introduction
Now you have learned about CSS and their applications. During this week student
will learn the concept of Dynamic HTML and applications of Dynamic HTML in web
sites. The learners will be able to apply dynamic behaviors for the web interfaces and
write basic scripts to improve the appearance. The learner will be able to learn the
script-programing concept with JavaScript.
Learning Outcomes:
After completion of this lesson, the learner will be able to design webpages with
basic dynamic behaviors and inline scripts by using inbuilt in JavaScript functions.
This lesson enables you to
• Describe dynamic behaviors of web interfaces
• Use dynamic html elements
• Identify the application of JavaScripts
• Write inline JavaScript using inbuilt functions
• Write inline JavaScripts to display text with calculations
• Write JavaScripts with html elements and attributes
Lesson Outline
• What is DHTML?
• Inbuilt dynamic HTML elements
• Scripts and light weight interpreted programs
• How web scripts work?
• What is Javascripts?
• Client side programing
• Inline JavaScript sintax
• Basic functions and their usage
• Wrtite procedures with JavaScripts
• Variables, data types and calculations
• Buttons and Functions
8.1 What is DHTML?
Up to the previous lesson, you learned about static web content designing. However,
static web does not attract the audience. Dynamic behavior of web interfaces is
categorized into two parts called server-side dynamic web interfaces and client-side
dynamic web interfaces. Here we learn about client-side dynamic web interfaces.
Dynamic behaviors such as displaying system time, verification of user inputs, user
environment scanning etc., are done using client side scripts.
A client-side dynamic webpage processes the web page using HTML script running in
the browser as it loads. The way HTML page loads is determined by the JavaScript
and other scripting languages, so outcome of HTML page is differ according to the
Script. Dynamic HTML or DHTML is a collective term for a combination of Hypertext
Markup Language (HTML) tags and options use collection of technologies together to
create user interactive and animated web pages, that can make Web pages more
animated and interactive. Much of dynamic HTML is specified in HTML 4.0. Simple
examples of dynamic HTML capabilities include having the color of a text heading
change when a user passes a mouse over it and allowing a user to "drag and drop" an
image to another place on a Web page. Dynamic HTML can allow Web documents to
look and act like desktop applications or multimedia productions. IT uses
combination of static markup language (eg: HTML), client-side scripting language (Eg:
JavaScript), presentation definition language (eg: CSS) and Document Object Model
(DOM).
<script>
document.write(Date());
</script>
</body>
</html>
Activity 8.1: Create the above web script and observe the output.
<script>
document.getElementById("p1").innerHTML = "New text!";
</script>
</body>
</html>
Activity 8.2: Create the above web script and observe the output.
<!DOCTYPE html>
<html>
<body> Here “Old Heading” has been
replaced with “New Heading”
<h1 id="id01">Old Heading</h1>
<script>
var element = document.getElementById("id01");
element.innerHTML = "New Heading";
</script>
</body>
</html>
Activity 8.3: Create the above web script and observe the output.
<!DOCTYPE html>
<html> Here src attribute of myImage
<body> “smiley.gif” has been replaced
with “landscape.jpg”
<img id="myImage" src="smiley.gif">
<script>
document.getElementById("myImage").src = "landscape.jpg";
</script>
</body>
</html>
Activity 8.4: Create the above web script and observe the output. Provide an image
appropriately.
8.3 Scripts and light weight interpreted programs
A scripting or script language is a programming language that supports scripts:
programs written for a special run-time environment that automate the execution of
tasks that could alternatively be executed one-by-one by a human operator. Scripting
languages are often interpreted (rather than compiled). An interpreted language is a
type of programming language for which most of its implementations execute
instructions directly and freely, without previously compiling a program into
machine-language instructions. The interpreter executes the program directly,
translating each statement into a sequence of one or more subroutines already
compiled into machine code.
<head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</head>
Here “onclick” event call the “myFunction()”
method in the JavaScript embedded in head tag
<body>
</body>
</html>
Activity 8.5: Create the above web script and observe the output.
8.7.2 Inline JavaScript embedded in <body>
<!DOCTYPE html>
<html>
<body> Here “onclick” event call the “myFunction()”
method in the JavaScript embedded in body tag
<h1>A Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
Here is the JavaScript embedded in body tag
</body>
</html>
Activity 8.6: Create the above web script and observe the output.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>This example calls a function which performs a calculation, and returns the result:</p>
Activity 8.7: Create the above web script and observe the output.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Procedures</h2>
<p>This example guide you step by step procedure:</p>
<p id="demo"></p>
<script>
var x=10;// step 1
var y=20;// step 2
var ans;// step 3
ans=x+y;// step 4
document.getElementById("demo").innerHTML =
"Addtion of x and y is "+ans;//step 5
</script>
</body>
</html>
Activity 8.8: Create the above web script and observe the output.
JavaScript variables can hold many data types: numbers, strings, objects and more:
• Boolean
• Number
• String
• Object
8.10.1Boolean
Pretty standard across all languages, booleans are true and false. They're often used
for conditional statements.
8.10.2 Number
The number data type covers integers and floats. That is, the number type can
handle normal numbers (1, 2, 3, 4), but also negative numbers and decimal places.
This is different from many languages that have multiple data types to support
different numbers.
var num = 1;
typeof num; // number
8.10.3 String
As in most languages, JS strings are groupings of characters.
"hello world";
"I like cats";
'Testing "quotes';
I don’t think they're particularly interesting, but they are remarkably powerful. The
main way we communicate with our users is the written word and string makes this
possible.
8.10.4 Object
Everything in JS that we didn’t discuss above is an Object. So objects are the most
complex data type. They typically look like this:
var cat = { sound: "meow" };
But notice that we haven’t mentioned Array, Date, or even function that’s
because, officially, they're all of type object
8.10.5 The Concept of Data Types
var x = 16 + "Volvo";
Does it make any sense to add "Volvo" to sixteen? Will it produce an error or will it
produce a result?
When adding a number and a string, JavaScript will treat the number as a string.
Example:
var x = 16 + "Volvo";
Try it Yourself » (click the text)
Example
var x = "Volvo" + 16;
Try it Yourself » (Click the text)
JavaScript evaluates expressions from left to right. Different sequences can produce
different results:
JavaScript:
var x = 16 + 4 + "Volvo";
Result:
20Volvo
Try it Yourself » (Click the text)
JavaScript:
var x = "Volvo" + 16 + 4;
Result:
Volvo164
Try it Yourself » (Click the text)
In the first example, JavaScript treats 16 and 4 as numbers, until it reaches "Volvo".
In the second example, since the first operand is a string, all operands are treated as
strings.
JavaScript has dynamic types. This means that the same variable can be used to hold
different data types:
Example
var x; // Now x is undefined
x = 5; // Now x is a Number
x = "John"; // Now x is a String
Try it yourself » (Click the text)
8.10.7 JavaScript Strings
Strings are written with quotes. You can use single or double quotes:
8.10.8 Example
var carName = "Volvo XC60"; // Using double quotes
var carName = 'Volvo XC60'; // Using single quotes
Try it yourself » (Click the text)
You can use quotes inside a string, as long as they don't match the quotes
surrounding the string:
Example
var answer = "It's alright"; // Single quote inside double quotes
var answer = "He is called 'Gamini'"; // Single quotes inside double quotes
var answer = 'He is called "Gamini"'; // Double quotes inside single quotes
Try it yourself » (Click the text)
8.10.9 JavaScript Numbers
Example
var x1 = 34.00; // Written with decimals
var x2 = 34; // Written without decimals
Try it yourself » (Click the text)
Extra large or extra small numbers can be written with scientific (exponential)
notation:
Example
var y = 123e5; // 12300000
var z = 123e-5; // 0.00123
Try it yourself » (Click the text)
8.10.10 JavaScript Booleans
Example
var x = 5;
var y = 5;
var z = 6;
(x == y) // Returns true
(x == z) // Returns false
onclick=JavaScript
</body>
</html>
Not try the above example with function call. Use following code segment.
<!DOCTYPE html> Here this “onclick” event call the
<html> changeText(id) function with it’s ID
<body>
<script>
function changeText(id) { This JavaScript function change
id.innerHTML = "Ooops!"; the text of the HTML to “Ooops!”
}
</script>
</body>
</html>
Activity 8.9: Create the above web script and observe the output.