Cs 4 1 PHP
Cs 4 1 PHP
iq
❖ CSS:
➢ CSS stands for Cascading Style Sheets.
➢ CSS describes how HTML elements are to be displayed.
➢ CSS saves a lot of work. It can control the layout of multiple web pages all at
once.
➢ External stylesheets are stored in CSS files
Ex:
p {
text-align: center;
color: red;
}
• The id Selector:
➢ The id selector uses the id attribute of an HTML element to select a specific
element.
➢ The id of an element should be unique within a page, so the id selector is used to
select one unique element!
➢ To select an element with a specific id, write a hash (#) character, followed by
the id of the element.
➢ The style rule below will be applied to the HTML element with id="para1":
Ex:
#para1 {
text-align: center;
color: red;
}
You can also specify that only specific HTML elements should be affected by a
class. In the example below, only <p> elements with class="center" will be center-
aligned:
Ex:
p.center {
text-align: center;
color: red;
}
HTML elements can also refer to more than one class. In the example below, the
<p> element will be styled according to class="center" and to class="large":
Ex:
<p class="center large">This paragraph refers to two classes.</p>
• Grouping Selectors:
If you have elements with the same style definitions, It will be better to group
the selectors, to minimize the code.
Ex:
h1, h2, p {
text-align: center;
color: red;
}
• CSS Comments:
A CSS comment starts with /* and ends with */. Comments can also span multiple
lines:
Ex:
p {
color: red;
/* This is a single-line comment */
text-align: center;
}
/* This is
a multi-line
comment */
➢ An external style sheet can be written in any text editor. The file should not
contain any html tags. The style sheet file must be saved with a .css extension.
➢ Here is how the "mystyle.css" looks:
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Note: Do not add a space between the property value and the unit (such as margin-
left: 20 px;). The correct way is: margin-left: 20px;
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
Instructor: Sudad H. Abed / Email: [email protected]
• Inline Styles:
➢ An inline style may be used to apply a unique style for a single element.
➢ To use inline styles, add the style attribute to the relevant element. The style
attribute can contain any CSS property.
➢ The example below shows how to change the color and the left margin of a <h1>
element:
Ex:
<h1 style="color:blue;margin-left:30px;">This is a heading</h1>
Ex:
Assume that an external style sheet has the following style for the <h1>
element:
h1 {
color: navy;
}
then, assume that an internal style sheet also has the following style for the <h1>
element:
h1 {
color: orange;
}
If the internal style is defined after the link to the external style sheet, the <h1>
elements will be "orange":
Ex:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<style>
h1 {
color: orange;
}
</style>
</head>
Instructor: Sudad H. Abed / Email: [email protected]
However, if the internal style is defined before the link to the external style sheet,
the <h1> elements will be "navy":
Ex:
<head>
<style>
h1 {
color: orange;
}
</style>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
• Cascading Order:
What style will be used when there is more than one style specified for an HTML
element?
Generally speaking we can say that all the styles will "cascade" into a new "virtual"
style sheet by the following rules, where number one has the highest priority:
1. Inline style (inside an HTML element)
2. External and internal style sheets (in the head section)
3. Browser default
So, an inline style (inside a specific HTML element) has the highest priority, which
means that it will override a style defined inside the <head> tag, or in an external
style sheet, or a browser default value.
❖ CSS Backgrounds:
The CSS background properties are used to define the background effects for
elements.
CSS background properties:
✓ background-color
✓ background-image
✓ background-repeat
✓ background-attachment
✓ background-position
Instructor: Sudad H. Abed / Email: [email protected]
• Background-Color:
➢ The background-color property specifies the background color of an element.
The background color of a page is set like this:
Ex:
body {
background-color: lightblue;
}
➢ background-repeat:
➢ By default, a background-image is repeated both vertically and horizontally.
➢ The background-repeat property sets if/how a background image will be
repeated.
background-repeat: no-repeat; /* No repeat */
background-repeat: repeat-y; /* Repeat vertically */
background-repeat: repeat-x; /* Repeat horizontally */
Ex:
body {
background-image: url("paper.gif");
background-repeat: no-repeat;
}
Instructor: Sudad H. Abed / Email: [email protected]
• background- attachment:
❖ CSS Borders:
The CSS border properties allow you to specify the style, width, and color of an
element's border.
• Border Style:
The border-style property specifies what kind of border to display.
✓ dotted - Defines a dotted border
✓ dashed - Defines a dashed border
✓ solid - Defines a solid border
✓ double - Defines a double border
✓ groove - Defines a 3D grooved border. The effect depends on the border-color
value
✓ ridge - Defines a 3D ridged border. The effect depends on the border-color
value
✓ inset - Defines a 3D inset border. The effect depends on the border-color value
✓ outset - Defines a 3D outset border. The effect depends on the border-color
value
✓ none - Defines no border
✓ hidden - Defines a hidden border
The border-style property can have from one to four values (for the top border, right
border, bottom border, and the left border).
Instructor: Sudad H. Abed / Email: [email protected]
Ex:
p.dotted {border-style: dotted;}
p.dashed {border-style: dashed;}
p.solid {border-style: solid;}
p.double {border-style: double;}
p.groove {border-style: groove;}
p.ridge {border-style: ridge;}
p.inset {border-style: inset;}
p.outset {border-style: outset;}
p.none {border-style: none;}
p.hidden {border-style: hidden;}
p.mix {border-style: dotted dashed solid double;}
• Border Width:
➢ The border-width property specifies the width of the four borders.
➢ The width can be set as a specific size (in px, pt, cm, em, etc) or by using one of
the three pre-defined values: thin, medium, or thick.
➢ The border-width property can have from one to four values (for the top border,
right border, bottom border, and the left border).
Instructor: Sudad H. Abed / Email: [email protected]
Ex:
p.one {
border-style: solid;
border-width: 5px;
}
p.two {
border-style: solid;
border-width: medium;
}
p.three {
border-style: solid;
border-width: 2px 10px 4px 20px;
}
• Border Color:
➢ The border-color property can have from one to four values (for the top border,
right border, bottom border, and the left border).
➢ If border-color is not set, it inherits the color of the element.
Ex:
p.one {
border-style: solid;
border-color: red;
}
p.three {
border-style: solid;
border-color: red green blue yellow;
}
You can also specify all the individual border properties for just one side:
Ex:
p {
border-left: 6px solid red;
background-color: lightgrey;
}
Instructor: Sudad H. Abed / Email: [email protected]
❖ CSS Margins:
The CSS margin properties are used to create space around elements, outside of any
defined borders.
With CSS, you have full control over the margins. There are properties for setting
the margin for each side of an element (top, right, bottom, and left).
• Margin - Individual Sides:
CSS has properties for specifying the margin for each side of an element:
✓ margin-top
✓ margin-right
✓ margin-bottom
✓ margin-left
Ex:
p {
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
❖ CSS Padding:
➢ The CSS padding properties are used to generate space around an element's
content, inside of any defined borders.
➢ With CSS, you have full control over the padding. There are properties for setting
the padding for each side of an element (top, right, bottom, and left).
• Padding - Individual Sides:
CSS has properties for specifying the padding for each side of an element:
✓ padding-top Ex:
✓ padding-right div {
✓ padding-bottom padding-top: 50px;
padding-right: 30px;
✓ padding-left padding-bottom: 50px;
padding-left: 80px;
}
Instructor: Sudad H. Abed / Email: [email protected]
❖ CSS Font:
The CSS font properties define the font family, boldness, size, and the style of a
text.
• Font Family:
• Font Style:
➢ The font-style property is mostly used to specify italic text.
Ex:
p.normal {
font-style: normal;
}
p.italic {
font-style: italic;
}
• Font Size:
➢ The font-size property sets the size of the text.
➢ Set font size with pixels.
Ex:
p {
font-size: 14px;
}
Instructor: Sudad H. Abed / Email: [email protected]
• Font Weight:
➢ The font-weight property specifies the weight of a font:
Ex:
p.normal {
font-weight: normal;
}
p.thick {
font-weight: bold;
}
❖ CSS Links:
➢ With CSS, links can be styled in different ways.
• Styling Links:
➢ Links can be styled with any CSS property (e.g. color, font family, background,
etc.).
Ex:
a {
color: hotpink;
}
➢ In addition, links can be styled differently depending on what state they are in.
The four links states are:
• a:link - a normal, unvisited link.
• a:visited - a link the user has visited.
• a:hover - a link when the user mouses over it.
• a:active - a link the moment it is clicked.
Ex:
/* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
}
/* mouse over link */
a:hover {
color: hotpink;
}
Instructor: Sudad H. Abed / Email: [email protected]
/* selected link */
a:active {
color: blue;
}
Note: When setting the style for several link states, there are some order rules:
✓ a:hover MUST come after a:link and a:visited
✓ a:active MUST come after a:hover
a:hover, a:active {
background-color: red;
}
❖ CSS Tables:
➢ The look of an HTML table can be greatly improved with CSS:
• Table Borders:
➢ The example below specifies a black border for <table>, <th>, and <td>
elements:
Ex:
table, th, td {
border: 1px solid black;
}
• Table Borders:
➢ The border-collapse property sets whether the table borders should be
collapsed into a single border:
Instructor: Sudad H. Abed / Email: [email protected]
Ex:
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
• Table Horizontal Alignment:
➢ The text-align property sets the horizontal alignment (like left, right, or center)
of the content in <th> or <td>.
➢ By default, the content of <th> elements are center-aligned and the content of
<td> elements are left-aligned.
Ex:
th {
text-align: left;
}
• Hoverable Table:
➢ Use the :hover selector on <tr> to highlight table rows on mouse over:
Ex:
tr:hover {background-color: #f5f5f5;}
• Striped Tables:
➢ For zebra-striped tables, use the nth-child() selector and add a background-
color to all even (or odd) table rows:
Ex:
tr:nth-child(even) {background-color: #f2f2f2;}
Instructor: Sudad H. Abed / Email: [email protected]
• Responsive Table:
➢ A responsive table will display a horizontal scroll bar if the screen is too small
to display the full content:
➢ Add a container element (like <div>) with overflow-x:auto around the <table>
element to make it responsive:
Ex:
<div style="overflow-x:auto;">
<table>
... table content ...
</table>
</div>
❖ CSS Position:
➢ The position property specifies the type of positioning method used for an
element.
There are five different position values:
• static
• relative
• fixed
• absolute
• sticky
Elements are then positioned using the top, bottom, left, and right properties.
However, these properties will not work unless the position property is set first.
They also work differently depending on the position value.
position: static;
➢ HTML elements are positioned static by default.
➢ Static positioned elements are not affected by the top, bottom, left, and right
properties.
➢ An element with position: static; is not positioned in any special way; it is
always positioned according to the normal flow of the page:
Ex:
div.static {
position: static;
border: 3px solid #73AD21;
}
Instructor: Sudad H. Abed / Email: [email protected]
position: relative;
➢ An element with position: relative; is positioned relative to its normal position.
➢ Setting the top, right, bottom, and left properties of a relatively-positioned
element will cause it to be adjusted away from its normal position. Other content
will not be adjusted to fit into any gap left by the element.
Ex:
div.relative {
position: relative;
left: 30px;
border: 3px solid #73AD21;
}
position: fixed;
➢ An element with position: fixed; is positioned relative to the viewport, which
means it always stays in the same place even if the page is scrolled. The top, right,
bottom, and left properties are used to position the element.
➢ A fixed element does not leave a gap in the page where it would normally have
been located.
Ex:
div.fixed {
position: fixed;
bottom: 0;
right: 0;
width: 300px;
border: 3px solid #73AD21;
}
position: absolute;
➢ An element with position: absolute; is positioned relative to the nearest
positioned ancestor (instead of positioned relative to the viewport, like fixed).
➢ However; if an absolute positioned element has no positioned ancestors, it uses
the document body, and moves along with page scrolling.
➢ Note: A "positioned" element is one whose position is anything except static.
Instructor: Sudad H. Abed / Email: [email protected]
Ex:
div.relative {
position: relative;
width: 400px;
height: 200px;
border: 3px solid #73AD21;
}
div.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
}
position: sticky;
➢ An element with position: sticky; is positioned based on the user's scroll position.
➢ A sticky element toggles between relative and fixed, depending on the scroll
position. It is positioned relative until a given offset position is met in the
viewport - then it "sticks" in place (like position:fixed).
➢ In this example, the sticky element sticks to the top of the page (top: 0), when
you reach its scroll position.
Ex:
div.sticky {
position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;
background-color: green;
border: 2px solid #4CAF50;
}
❖ HTML Forms:
The <input> element can be displayed in several ways, depending on the type
attribute.
Ex:
<form action="#" >
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname">
</form>
Instructor: Sudad H. Abed / Email: [email protected]
Ex:
<form action="#">
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
</form >
Ex:
<form action="#">
<textarea name="message" rows="10" cols="30">
The cat was playing in the garden.
</textarea>
</form >
➢ The rows attribute specifies the visible number of lines in a text area.
➢ The cols attribute specifies the visible width of a text area.
Ex:
<form action="#">
<button type="button" onclick="alert('Hello World!')">Click
Me!</button>
</form >
Instructor: Sudad H. Abed / Email: [email protected]
Ex:
<form action="/action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br><br>
<input type="submit" value="Submit">
</form>
➢ Depending on browser support, a color picker can show up in the input field.
Ex:
<form>
Select your favorite color:
<input type="color" name="favcolor">
<input type="submit">
</form>
2- Input Type Date:
The <input type="date"> is used for input fields that should contain a date.
Ex:
<form>
Birthday:
<input type="date" name="bday">
<input type="submit">
</form>
Instructor: Sudad H. Abed / Email: [email protected]
➢ A disabled input field is unusable and un-clickable, and its value will not be sent
when submitting the form:
Ex:
<form action="#">
First name:<br>
<input type="text" name="firstname" value="John" disabled>
</form>
➢ The hint is displayed in the input field before the user enters a value.
➢ The placeholder attribute works with the following input types: text, search,
url, tel, email, and password.
Instructor: Sudad H. Abed / Email: [email protected]
Ex:
<form action="#">
<input type="text" name="fname" placeholder="First name"><br>
<input type="text" name="lname" placeholder="Last name"><br>
<input type="submit" value="submit">
</form>
➢ The required attribute works with the following input types: text, search, url,
tel, email, password, date pickers, number, checkbox, radio, and file.
Ex:
<form action="#">
Username: <input type="text" name="usrname" required><br>
<input type="submit" value="submit">
</form>
JavaScript
JavaScript: Scripting language which is used to enhance the functionality and
appearance of web pages.
❖ JavaScript Where To
• The <script> Tag
In HTML, JavaScript code must be inserted between <script> and </script> tags.
Ex:
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>
➢ JavaScript Functions and Events
✓ A JavaScript function is a block of JavaScript code, that can be executed
when "called" for.
✓ A function can be called when an event occurs, like when the user clicks a
button.
• JavaScript in <head> or <body>
✓ You can place any number of scripts in an HTML document.
✓ Scripts can be placed in the <body>, or in the <head> section of an HTML page,
or in both.
Ex:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Instructor: Sudad H. Abed / Email: [email protected]
Note: placing scripts at the bottom of the <body> element improves the display
speed, because script compilation slows down the display.
• External JavaScript
Scripts can also be placed in external files:
Ex:
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
✓ External scripts are practical when the same code is used in many different web
pages.
✓ JavaScript files have the file extension .js.
✓ To use an external script, put the name of the script file in the src (source)
attribute of a <script> tag:
Ex:
<!DOCTYPE html>
<html>
<body>
<script src="myScript.js"></script>
</body>
</html>
• Using innerHTML
✓ To access an HTML element, JavaScript can use the document.getElementById
(id) method.
✓ The id attribute defines the HTML element. The innerHTML property defines the
HTML content:
Ex:
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
• Using document.write()
For testing purposes, it is convenient to use document.write():
Ex:
<script>
document.write(5 + 6);
</script>
• Using window.alert()
You can use an alert box to display data:
Ex:
<script>
window.alert(5 + 6);
</script>
• Using console.log()
For debugging purposes, you can use the console.log() method to display data.
Ex:
<script>
console.log(5 + 6);
</script>
Instructor: Sudad H. Abed / Email: [email protected]
❖ JavaScript Comments
✓ Single line comments start with //.
✓ Multi-line comments start with /* and end with */.
❖ JavaScript Variables
✓ All JavaScript variables must be identified with unique names.
✓ These unique names are called identifiers.
✓ A variable declared without a value will have the value undefined.
If you put a number in quotes, the rest of the numbers will be treated as strings, and
concatenated.
Ex:
var x = "5" + 2 + 3
❖ 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).
✓ Inside the function, the arguments (the parameters) behave as local variables.
Instructor: Sudad H. Abed / Email: [email protected]
❖ JavaScript Objects
✓ In real life, a car is an object.
✓ A car has properties like weight and color, and methods like start and stop:
• JavaScript Objects
This code assigns a simple value (Fiat) to a variable named car:
var car = "Fiat";
Objects are variables too. But objects can contain many values.
This code assigns many values (Fiat, 500, white) to a variable named car:
The values are written as name:value pairs (name and value separated by a colon).
Or
objectName["propertyName"] Ex: person["lastName"];
• Accessing Object Methods
✓ A method is actually a function definition stored as a property value.
You access an object method with the following syntax:
objectName.methodName() Ex: name = person.fullName();
Instructor: Sudad H. Abed / Email: [email protected]
❖ JavaScript Scope
Scope determines the accessibility (visibility) of variables.
In JavaScript there are two types of scope:
• Local scope
• Global scope
JavaScript has function scope: Each function creates a new scope.
• Local JavaScript Variables
✓ Variables declared within a JavaScript function, become LOCAL to the function.
✓ Local variables have local scope: They can only be accessed within the function.
Ex:
// code here can not use carName
function myFunction() {
var carName = "Volvo";
// code here can use carName
}
Ex:
var carName = " Volvo";
// code here can use carName
function myFunction() {
// code here can use carName
}
❖ JavaScript Events
✓ HTML events are "things" that happen to HTML elements.
✓ When JavaScript is used in HTML pages, JavaScript can "react" on these events.
• HTML Events
Here are some examples of HTML events:
• An HTML web page has finished loading
• An HTML input field was changed
• An HTML button was clicked
In the next example, the code changes the content of its own element
(using this.innerHTML):
Ex:
<button onclick="this.innerHTML = Date()">The time is?</button>
Instructor: Sudad H. Abed / Email: [email protected]
Event handlers can be used to handle, and verify, user input, user actions, and
browser actions:
• Things that should be done every time a page loads
• Things that should be done when the page is closed
• Action that should be performed when a user clicks a button
• Content that should be verified when a user inputs data
• And more ..
Instructor: Sudad H. Abed / Email: [email protected]
Method Description
Note:
✓ Both the indexOf(), and the lastIndexOf() methods return -1 if the text is not found.
Instructor: Sudad H. Abed / Email: [email protected]
• Number Properties
Property Description
The JavaScript Math object allows you to perform mathematical tasks on numbers.
Method Description
1- The if Statement
Use the if statement to specify a block of JavaScript code to be executed if a
condition is true.
Ex:
if (hour < 18) {
greeting = "Good day";
}
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
❖ JavaScript Arrays
• Creating an Array
Syntax:
var array_name = [item1, item2, ...];
• Access the Elements of an Array
✓ You refer to an array element by referring to the index number.
✓ This statement accesses the value of the first element in cars:
var name = cars[0];
Ex:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.length; // the length of fruits is 4
✓ Looping Array Elements
The best way to loop through an array, is using a "for" loop:
Ex:
<p id="demo"></p>
<script>
var fruits, text, fLen, i;
fruits = ["Banana", "Orange", "Apple", "Mango"];
fLen = fruits.length;
text = "<ul>";
for (i = 0; i < fLen; i++) {
text += "<li>" + fruits[i] + "</li>";
}
text += "<ul>";
</script>
New element can also be added to an array using the length property:
Ex:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits[fruits.length] = "Lemon";
Instructor: Sudad H. Abed / Email: [email protected]
• Associative Arrays
Ex:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.toString();
Result: Banana,Orange,Apple,Mango
The join() method also joins all array elements into a string.
It behaves just like toString(), but in addition you can specify the separator:
Ex:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.join(" * ");
Result: Banana * Orange * Apple * Mango
Instructor: Sudad H. Abed / Email: [email protected]
When you work with arrays, it is easy to remove elements and add new elements.
Popping: The pop() method removes the last element from an array:
Ex:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop(); // Removes the last element ("Mango") from fruit
The pop() method returns the value that was "popped out":
Ex:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = fruits.pop(); // the value of x is "Mango"
Pushing: The push() method adds a new element to an array (at the end):
Ex:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi"); // Adds a new element ("Kiwi") to fruits
Ex:
• Changing Elements
Ex:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits[0] = "Kiwi"; // Changes the first element of fruits to "Kiwi"
Instructor: Sudad H. Abed / Email: [email protected]
• Deleting Elements
Since JavaScript arrays are objects, elements can be deleted by using the JavaScript
operator delete:
Ex:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
delete fruits[0];
// Changes the first element in fruits to undefined
• Splicing an Array
• The splice() method can be used to add new items to an array:
Ex:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi");
Result: Banana,Orange,Lemon,Kiwi,Apple,Mango
✓ The first parameter (2) defines the position where new elements should
be added (spliced in).
✓ The second parameter (0) defines how many elements should be removed.
✓ The rest of the parameters ("Lemon”, "Kiwi") define the new elements to
be added.
• Merging (Concatenating) Arrays
Ex:
var myGirls = ["Cecilie", "Lone"];
var myBoys = ["Emil", "Tobias", "Linus"];
var myChildren = myGirls.concat(myBoys);
• Sorting an Array
The sort() method sorts an array alphabetically:
Ex:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort(); // Sorts the elements of fruits
output: Apple,Banana,Mango,Orange
• Reversing an Array
The reverse() method reverses the elements in an array.
Ex:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort(); // Sorts the elements of fruits
fruits.reverse(); // Reverses the order of the elements
output: Orange,Mango,Banana,Apple
✓ 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.
Syntax:
window.alert("sometext");
The window.alert() method can be written without the window prefix.
Ex:
alert("I am an alert box!");
✓ To display line breaks inside a popup box, use a back-slash followed by the character
n.
Ex:
alert("Hello\nHow are you?");
Instructor: Sudad H. Abed / Email: [email protected]
• 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.
Syntax:
window.confirm("sometext");
Ex:
• 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.
Syntax:
window.prompt("sometext","defaultText");
Ex:
var person = prompt("Please enter your name", "Harry Potter");
if (person == null || person == "") {
txt = "User cancelled the prompt.";
} else {
txt = "Hello " + person + "! How are you today?";
}
❖ Timing Events
✓ The window object allows execution of code at specified time intervals.
✓ These time intervals are called timing events.
✓ The two key methods to use with JavaScript are:
➢ setTimeout(function, milliseconds)
Executes a function, after waiting a specified number of milliseconds.
➢ setInterval(function, milliseconds)
Same as setTimeout(), but repeats the execution of the function
continuously.
• The setTimeout() Method
window.setTimeout(function, milliseconds);
✓ The window.setTimeout() method can be written without the window prefix.
✓ The first parameter is a function to be executed.
✓ The second parameter indicates the number of milliseconds before execution.
Ex:
<button onclick="setTimeout(myFunction, 3000)">Try it</button>
<script>
function myFunction() {
alert('Hello');
}
</script>
Instructor: Sudad H. Abed / Email: [email protected]
window.setInterval(function, milliseconds);
✓ The window.setInterval() method can be written without the window prefix.
✓ The first parameter is the function to be executed.
✓ The second parameter indicates the length of the time-interval between each
execution.
Ex:
var myVar = setInterval(myTimer, 1000);
function myTimer() {
var d = new Date();
document.getElementById("demo").innerHTML =
d.toLocaleTimeString();
}