JavaScript: Writing Code for Interactive Web Pages, Exercises of Information Technology

Examples and explanations of javascript code embedded in html to create interactive web pages. Topics include using browser objects and functions for writing on the page, mathematical and logical operators, and creating a simple calculator.

Typology: Exercises

2011/2012

Uploaded on 08/11/2012

duraid
duraid 🇮🇳

4.3

(3)

72 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
E
E-
-C
CO
OM
MM
ME
ER
RC
CE
E
I
IT
T4
43
30
0
V
VU
U
© Copyright Virtual University of Pakistan 57
Lesson 13
JAVA SCRIPTING
Java script examples
<HTML>
<HEAD>
<TITLE>Javascript Example</TITLE>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--alert("Thank you for visiting my web site!")
//-->
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
Result is shown in Fig. 1 below.
Fig. 1
Note we can embed JavaScript code/instructions in the plain HTML code with the help of <script> and
</script> tags. In the above example ‘alert’ is the function/method of a predefined browser object that is,
window object, and is used to create alert boxes or popup messages on the window. ‘//’ is used in
JavaScript to provide comments. Here, we use ‘//’ so that old browsers that do not support JavaScript treat
information within the Script tag as comments.
Writing on the page
<HTML>
<HEAD>
<TITLE>example-writing on the page</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
document.write("Hello! Thank you for visiting my web site.")
//-->
</SCRIPT>
</BODY>
docsity.com
pf3
pf4

Partial preview of the text

Download JavaScript: Writing Code for Interactive Web Pages and more Exercises Information Technology in PDF only on Docsity!

Lesson 13 JAVA SCRIPTING

Java script examples

Javascript Example

Result is shown in Fig. 1 below.

Fig. 1

Note we can embed JavaScript code/instructions in the plain HTML code with the help of tags. In the above example ‘alert’ is the function/method of a predefined browser object that is, window object, and is used to create alert boxes or popup messages on the window. ‘//’ is used in JavaScript to provide comments. Here, we use ‘//’ so that old browsers that do not support JavaScript treat information within the Script tag as comments. Writing on the page

example-writing on the page

docsity.com

Result is shown in Fig. 2 below.

Fig. 2

Here, ‘document’ is a browser object and ‘write’ is its function which is used to write text on the page. Browser objects are loaded by a JavaScript capable browser to provide access to the web page and the HTML elements it contains. These objects are used to update and interact with the loaded web page.

Operators in java script Mathematical operators ‘+’ For addition of two values ‘-‘ for subtraction of two values ‘*’ for multiplication ‘/’ for division ‘%’ modulus (for calculating the remainder) ‘++’ for increment ‘--‘ for decrement Logical operators ‘&&’ for logical and ‘||’ for logical or ‘!’ for logical not

Comparison operators ‘==’ for Equal ‘!=’ for not equal ‘< ‘ for Less than ‘<=’ for less than equal ‘>’ for Greater than ‘>=’ for Greater than equal

Functions in javascript

A variable in JavaScript can be defined using the keyword ‘var’ and a function by the Keyword ‘function. A function can be defined in the following format: function myfunction() { // some code } Here, ‘myfunction’ is the name of the function. Creating a calculator in JavaScript

My Simple Calculator

Add and Subtract Calculator

First Number: docsity.com

Add Subtract Calculator

First Number:

Second Number:

Result:

Result is shown in Fig. 5 below in a text box, if you type nos. 125 and 66 in the two text boxes, respectively, and click ‘Add them!!’.

Fig. 5

docsity.com