Javascript: Prof.N.Nalini Ap (SR) Scope Vit
Javascript: Prof.N.Nalini Ap (SR) Scope Vit
Javascript: Prof.N.Nalini Ap (SR) Scope Vit
Prof.N.Nalini
AP(Sr)
SCOPE
VIT
Introduction
A scripting languages are a form of
programming language that are "embedded" in
Web pages and interpreted at run time.
Executed by an interpreter contained within the
web browser (scripting host)
Interpreter uses a scripting engine
Converts code to executable format each time it runs
Converted when browser loads web document
Scripting Language
Two formats:
Client-side
Program runs on client
(browser)
Server-side
Program runs on server
JavaScript’s role on the Web
HTML
Purpose tell the browser how a document should appear
Static can view or print (no interaction)
JavaScript
Purpose make web pages (documents) more dynamic and
interactive
Change contents of document, provide forms and
controls, animation, control web browser window, etc.
The JavaScript
JavaScript: the first Web scripting language,
developed by Netscape in 1995
syntactic similarities to Java/C++, but simpler & more flexible
(loose typing, dynamic variables, simple objects)
EditGrid
http://www.editgrid.com/
A Simple Script
<html>
<head><title>First JavaScript Page</title></head>
<body>
<h1>First JavaScript Page</h1>
<script type="text/javascript">
document.write("<hr>");
document.write("Hello World Wide Web");
document.write("<hr>");
</script>
</body>
</html>
Embedding JavaScript
<html>
<head><title>First JavaScript Program</title></head>
<body>
<script type="text/javascript"
src="your_source_file.js"></script>
</body>
</html> Inside your_source_file.js
document.write("<hr>");
document.write("Hello World Wide Web");
document.write("<hr>");
<head>
<script language=“JavaScript”>
Variable declaration
var id;
id = prompt(“What is your student id number?”);
alert(id);
name = prompt(“What is your name?”,”No name”);
alert(name);
</script>
</head>
Contains only 'A' – 'Z', 'a' – 'z', '0' – '9', '_', '$'
First character cannot be a digit
Case-sensitive
Cannot be reserved words or keywords
Variable and Variable Declaration
<head><script type="text/javascript">
// We are in the default scope – the "window" object.
x = 3; // same as "window.x = 3"
var y = 4; // same as "y = 4" or "window.y = 4"
</script></head>
20
Integer and Floating-point number
example
<script language=“JavaScript”>
var i = 100;
var f = 3.0e10;
// floating-point number 30000000000
document.write(i);
document.write(f);
</script>
The integer 100 and the number
30,000,000,000 will be appeared in the
browser window.
21
Boolean Values
A Boolean value is a logical value of either true
or false. (yes/no, on/off)
Often used in decision making and data
comparison.
In JavaScript, you can use the words “true” and
“false” directly to indicate Boolean values.
22
Boolean value example
<head>
<script language=“JavaScript”>
var result;
result = (true*10 + false*7);
alert(“true*10 + false*7 = “, result);
</script>
</head>
24
Strings example
<head>
<script language=“JavaScript”>
document.write(“This is a string.”);
document.write(“This string contains ‘quote’.”);
var myString = “My testing string”;
alert(myString);
</script>
</head>
<head>
<script language=“JavaScript”>
var x = “hello”, y;
alert(“Variable x value is “ + typeof(x));
alert(“Variable y value is “ + typeof(y));
alert(“Variable x value is “ + typeof(z));
</script>
</head>
28
Type Conversion (To Boolean)
The following values are treated as false
null
undefined
+0, -0, NaN (numbers)
"" (empty string)
Type Conversion
Converting a value to a number
var numberVar = someVariable – 0;
<script>
var x = prompt("What size should the ball's width be?")
document.getElementById("ball1").width = x
</script>
</body>
</html>
We can use getElementById to change
CSS style:
<!DOCTYPE html><html><head> <meta charset= "utf-8" /></head>
<body>
<p id = "firstp"> This is a paragraph</p>
<p id = "secondp">This is a second paragraph</p>
<script>
document.getElementById("firstp").style.color = "#9999FF";
document.getElementById("firstp").style.fontSize = "120%";
document.getElementById("firstp").style.backgroundColor="#332277";
document.getElementById("firstp").style.textAlign = "right";
document.getElementById("firstp").style.padding = "30px";
document.getElementById("firstp").style.borderWidth = "8px";
document.getElementById("firstp").style.borderColor = "green";
document.getElementById("firstp").style.borderStyle = "inset";
</script>
</body></html>
innerHTML