2.JS Part1
2.JS Part1
JAVASCRIPT
Objectives
by the end of lecture, students should be able to:
</script>
The <!-- and --> tags begin and end the HTML comment. This will prevent older
browsers from displaying the script, while allowing the script to work on
browsers that support it.
The // in the last line is a JavaScript comment—this prevents the HTML comment
tag from being detected as a JavaScript error.
JavaScript Syntax
Created with basic text editors.
JavaScript statements are placed within the <script>... </script> tag in
HTML web page.
You can place the <script> tag containing your JavaScript anywhere
within you web page but it is preferred way to keep it within the
<head> tags.
The <script> tag alert the browser program to begin interpreting all the
text between these tags as a script. So simple syntax of your JavaScript
will be as follows
The JavaScript interpreter that will be running your code will look line-
by-line through the code to see what to do.
In order for the interpreter to understand what you've written, there
are some rules you must follow, i.e., syntax.
JavaScript Statements
The <noscript> tag: Defines an alternate content for users that have
disabled scripts in their browser or have a browser that doesn't support
client-side scripts. E.gs.
<noscript>Sorry, your browser does not support JavaScript!</noscript>
<noscript>
Sorry...JavaScript is needed to go ahead.
</noscript>
JavaScript Syntax
(Continued)
Whitespace and Line Breaks:
JavaScript ignores spaces, tabs, and newlines that appear in JavaScript programs.
◦ you are free to format and indent your programs in a neat and consistent way that makes the code easy to read and
understand.
JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and
any other identifiers must always be typed with a consistent capitalization of letters.
E.g. identifiers Time, TIme and TIME will have different meanings in JavaScript.
NOTE: Care should be taken while writing your variable and function names in JavaScript.
Comments in JavaScript:
Any text between a // and the end of a line is treated as a comment and is ignored by JavaScript
Any text between the characters /* and */ is treated as a comment. This may span multiple lines
JavaScript also recognizes the HTML comment opening sequence <!--. JavaScript treats this as a single-line
comment, just as it does the // comment.
The HTML comment closing sequence --> is not recognized by JavaScript so it should be written as //-->
JavaScript Syntax
(Continued)
JavaScript Placement in HTML File:
Script in <head>...</head> section.
E.g.
<html>
<head>
<script type="text/javascript" src="filename.js" ></script>
</head>
<body>
.......
</body>
</html>
Screen Output & Keyboard Input
1. alert("Hej! \n");
An alert is a box that pops
up to give the user a
- Parameter is plain text, not HTML
- Opens a dialog box which displays the message. Here's code for an
parameter string and an OK button alert that displays the
- It waits for the user to press the OK button message "Thanks for your
input!"
2. confirm("Do you want to continue?"); A confirm box is often used
if you want the user to
- Opens a dialog box and displays the parameter
and two buttons, OK and Cancel
verify or accept something.
- Returns a Boolean value, depending on which
button was pressed (it waits for one) When a confirm box pops
up, the user will have to
3. prompt("What is your name?", ""); click either "OK" or "Cancel"
to proceed.
- Opens a dialog box and displays its string
parameter, along with a text box and two A prompt box asks the user
buttons, OK and Cancel for some information and
provides a response field for
- The second parameter is for a default response her answer.
if the user presses OK without typing a
response in the text box (waits for OK)
Your First JavaScript Script:
<html>
<body>
<script language="javascript" type="text/javascript">
<!--
document.write("Hello World!")
//-->
</script>
</body>
</html>
JavaScript Operators
Operator Description Example Result
x+y
- Subtraction x=5 3
y=2
x-y
* Multiplication x=5 20
y=4
x*y
/ Division 15/5 3
5/2 2,5
% Modulus (division 5%2 1
remainder)
10%8 2
10%2 0
++ Increment x=5 x=6
x++
-- Decrement x=5 x=4
x--
JavaScript Operators – 2
Assignment Operators Operator Example Is The Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y x=x%y
JavaScript Operators - 3
Operator Description Example
Logical Operators
&& and x=6
y=3
|| or x=6
y=3
! not x=6
y=3
<script>
alert("Hello World!")
</script>
Example
<script>
x=“Hello World!”
document.write(x)
</script>
<script>
x=“John Kofi….”
document.write(“Merhaba” +x)
</script> use line break html code
JavaScript Popup Boxes
Alert Box
◦ a JS command that pops up a dialog box with a message
◦ An alert box is often used if you want to make sure information comes
through to the user.
◦ When an alert box pops up, the user will have to click "OK" to proceed.
<script>
alert("Hello World!")
</script>
JavaScript Popup Boxes
-2
Confirm Box
◦ A confirm box is often used if you want the user to verify or accept
something.
◦ When a confirm box pops up, the user will have to click either "OK" or
"Cancel" to proceed.
◦ If the user clicks "OK", the box returns true. If the user clicks "Cancel", the
box returns false.
JavaScript Popup Boxes
-3
Prompt Box
◦ A prompt box is often used if you want the
user to input a value before entering a
page.
◦ When a prompt box pops up, the user will
have to click either "OK" or "Cancel" to
proceed after entering an input value.
◦ If the user clicks "OK“, the box returns the
input value. If the user clicks "Cancel“, the
box returns null.
Prompt Box Example
<script>
x=prompt (“Michael Edna”, “ ”)
document.write(“Merhaba <br>”,
+x)
</script>
JavaScript Introduction
Prompt window example:
Examples -2
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
Conditional Statements
Examples
<script>
x=3
if(x<0)
{
alert (“negative”)
}
else
{
alert (“positive”)
}
</script>
Conditional Statements
Examples
<script>
- 2
c=confirm(“are you a member?”)
if(c)
{
alert (“I am a member”)
}
else
{
alert (“I am not a member”)
}
</script>
Conditional Statements
Examples - 3
<script>
p=prompt(“animal name)
if(p==“Dog")
{
alert(“yes")
}
else
{
alert(“No")
}
</script>