Lab 7

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

JavaScript

Objective(s):

 To understand learn the language of the web javascript.


 How to work scripting language.

Tool(s) used:

For example: Brackets, Notepade++.

JavaScript:

JavaScript is a client-side as well as server side scripting language that can be inserted into
HTML pages and is understood by web browsers. JavaScript is also an Object Oriented
Programming language.

Dynamic web pages often require you to access and in some cases even manipulate things
beyond the HTML elements. JavaScript provides a rich set of objects and functions that allow
you to access information about the screen, browser window, history, and more.

JavaScript Comments:

JavaScript comments can be used to explain JavaScript code, and to make it more readable.
JavaScript comments can also be used to prevent execution, when testing alternative code.

Single Line Comments

Single line comments start with //. Any text between // and the end of the line will be ignored
by JavaScript (will not be executed).

Example:
This example uses a single-line comment before each code line:

// Change heading:
document.getElementById("myH").innerHTML = "My First
Page";
// Change paragraph:
document.getElementById("myP").innerHTML = "My first
paragraph.";
Multi-line Comments:
Multi-line comments start with /* and end with */. Any text between /* and */ will be ignored
by JavaScript.

Example:

This example uses a multi-line comment (a comment block) to explain the code:

/*
The code below will change
the heading with id = "myH"
and the paragraph with id = "myP"
in my web page:
*/

JavaScript variables are containers for storing data values.

In this example, x, y, and z, are variables:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p>In this example, x, y, and z are variables</p>
<p id="demo"></p>
<script>
var x = 5;
var y = 6;
var z = x + y;
document.getElementById("demo").innerHTML = z;
</script> </body> </html>

JavaScript Functions:
A JavaScript function is a block of code designed to perform a particular task.A JavaScript
function is executed when "something" invokes it (calls it). A JavaScript function is defined
with the function keyword, followed by a name, followed by parentheses ().

Function names can contain letters, digits, underscores, and dollar signs (same rules as
variables). The parentheses may include parameter names separated by commas:
(parameter1, parameter2, ...). The code to be executed, by the function, is placed inside curly
brackets: {}

function name(parameter1,parameter2,parameter3) {
code to be executed
}

Function Invocation

The code inside the function will execute when "something" invokes (calls) the function:

 When an event occurs (when a user clicks a button)


 When it is invoked (called) from JavaScript code
 Automatically (self invoked).

Types of Pop up boxes available in JavaScript:

 Alert box
 Confirm Box
 Prompt Box
Tasks:

Task 01. Time: 30 Minutes

JavaScript Can Change HTML Attributes.Write a code in which changes an HTML image by
changing the src (source) attribute of an <img> tag.

Figure 07-1

Task 02. Time: 30 Minutes

Write a function of JavaScript in which an alert message show on your screen in alert box.

Task 03. Time: 30 Minutes

Write JavaScript function in which JavaScript function is placed in the <head> section of an
HTML page. The function is invoked (called) when a button is clicked.
Figure 07-2

Task 04. Time: 30 Minutes

Write JavaScript code in which Change Box Color when mouse pointer enters the box and
when mouse pointer come out of the box.

You might also like