01 - Introduction To Javascript
01 - Introduction To Javascript
01 Introduction to JavaScript
Dr. Mostafa Elgendy
[email protected]
2
Agenda
❖ Introduction
❖ Variables
❖ Conditionals
❖ Summary
❖ Usability: can modify a page without having to post back to the server.
❖ Efficiency: can make quick changes to page without waiting for server.
❖ Event-driven: can respond to user actions like clicks and key presses.
❖ Security: has access to server's private data; client can't see source code
<html>
<head>
</head>
<body>
<script type="text/javascript">
document.write("Hello World")
</script>
</body>
</html>
JavaScript
❖ You may also have noticed that, unlike with PHP, there is no trailing
semicolon (;).
❖ If you wish to have more than one statement on a single line, you do
need to place a semicolon after each command except the last one
❖ You can put it in the <head> section, which is the ideal place if
you wish to execute a script when a page loads.
Variables
❖ The first character of a variable name can be only a–z, A–Z, $, or _ (no numbers).
❖ Names are case-sensitive. Count, count, and COUNT are all different variables.
❖ String Concatenation
❖ count = 42
❖ temperature = 98.4
❖ document.write(13 + 2)
Conditionals
if (a > 100)
{
b=2
document.write("a is greater than 100")
}
if (b == 10)
document.write("b is equal to 10")
JavaScript
❖ When a condition has not been met, you can execute an alternative by using an
else statement.
if (a > 100)
{
if (a > 100) document.write("a is greater than 100")
{ }
document.write("a is greater than 100") else if(a < 100)
} {
else document.write("a is less than 100")
{ }
document.write("a is less than or equal to 100") else
} {
JavaScript document.write("a is equal to 100")
} JavaScript
<script>
switch (page)
{
case "Home":
document.write("You selected Home")
break
case "About":
document.write("You selected About")
break
case "Login":
document.write("You selected Login")
break
case "Links":
document.write("You selected Links")
break
}
</script> JavaScript
IS388 - Web Programming 22-Mar-23
25
Looping
<script>
counter=0
while (counter < 5)
{
document.write("Counter: " + counter + "<br>")
++counter
}
</script> JavaScript
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
output
<script>
count = 1
do
{
document.write(count + " times 7 is " + count * 7 + "<br>")
} while (++count <= 7)
</script> JavaScript
1 times 7 is 7
2 times 7 is 14
3 times 7 is 21
4 times 7 is 28
5 times 7 is 35
6 times 7 is 42
7 times 7 is 49
output
<script>
for (count = 1 ; count <= 7 ; ++count)
{
document.write(count + "times 7 is " + count * 7 + "<br>");
}
</script> JavaScript
1 times 7 is 7
2 times 7 is 14
3 times 7 is 21
4 times 7 is 28
5 times 7 is 35
6 times 7 is 42
7 times 7 is 49
output
<script>
haystack = new Array()
haystack[17] = "Needle"
for (j = 0 ; j < 20 ; ++j)
{
if (haystack[j] == "Needle")
{
document.write("<br>- Found at location " + j)
break
}
else document.write(j + ", ")
}
</script> JavaScript
<script>
haystack = new Array()
haystack[4] = "Needle";
haystack[11] = "Needle"; 0, 1, 2, 3,
haystack[17] = "Needle" - Found at location 4
for (j = 0 ; j < 20 ; ++j) 5, 6, 7, 8, 9, 10,
{ - Found at location 11
if (haystack[j] == "Needle") 12, 13, 14, 15, 16,
{ - Found at location 17
document.write("<br>- Found at location " + j + "<br>") 18, 19
continue output
}
document.write(j + ", ")
}
</script> JavaScript
Access DOM
<!DOCTYPE html>
<html>
<body>
<h1 id="id1">My Heading 1</h1>
<button type="button" onclick =
"document.getElementById('id1').style.color =
'red'">Click Me!</button>
</body>
</html>
JavaScript
<!DOCTYPE html>
<html>
<body>
<div id='myobj'>Some text</div>
<script>
document.getElementById('myobj').style.color =
'green’
document.getElementById('myobj').style.fontStyle
= 'italic'
</script>
</body>
</html>
JavaScript
<html>
<head>
<title>DOM!!!</title>
</head>
<body>
<h1 id="one">Welcome</h1>
<p>This is the welcome message.</p>
<h2>Technology</h2>
<p>This is the technology section.</p>
<script type="text/javascript">
var text =
document.getElementById("one").innerHTML;
alert("The first heading is " + text);
</script>
</body>
</html>
JavaScript
<html>
<head>
<title>DOM!!!</title>
</head>
<body>
<h1 id="one">Welcome</h1>
<p>This is the welcome message.</p>
<h2>Technology</h2>
<p>This is the technology section.</p>
<script type="text/javascript">
document.getElementById("one").innerHTML="<b>Hel
lo World</b>";
</script>
</body>
</html>
JavaScript
<html>
<head>
<title>DOM!!!</title>
</head>
<body>
<h1>Welcome</h1>
<p>This is the welcome message.</p>
<h2>Technology</h2>
<p id="second">This is the technology section.</p>
<script type="text/javascript">
var paragraphs =
document.getElementsByTagName("p");
alert("Content in the second paragraph is " +
paragraphs[1].innerHTML);
</script>
</body>
</html>
JavaScript
IS388 - Web Programming 22-Mar-23
41
Example 6
<html>
<body>
<div id='object'>Div Object</div>
<script>
var div1 = document.getElementById('object').style
div1.border = 'solid 1px red’
div1.width = '200px'
div1.height = '200px’
div1.background = '#eee’
div1.color = 'blue'
div1.fontSize = '15pt'
div1.fontFamily = 'Helvetica’
div1.fontStyle = 'italic'
</script>
</body>
</html> JavaScript
❖ Introduction
❖ Variables
❖ Conditionals
❖ Summary