2 - js
TRANSCRIPT
-
7/27/2019 2 - js
1/10
-
7/27/2019 2 - js
2/10
JavaScript Popup Boxes
Alert Box
An alert box is often used if youwant to make sure information comes
through to the user.
Syntax: alert("sometext");
-
7/27/2019 2 - js
3/10
Continues.
Confirm Box
A confirm box is often used if you want theuser to verify or accept something. Returns
true or false.
Syntax:
confirm("sometext");
-
7/27/2019 2 - js
4/10
Continues
Prompt Box
A prompt box is often used if you wantthe user to input a value before entering
a page. Returns the input value or null.
Syntax:prompt("sometext","defaultvalue");
-
7/27/2019 2 - js
5/10
JavaScript Events
Events are actions that can be detected by
JavaScript.
Examples of events:A mouse click
A web page or an image loading
Mouse over a hot spot on the web page
Selecting an input field in an HTML form
Submitting an HTML form
A keystroke
Note: Events are normally used in combination with functions, and the
function will not be executed before the event occurs!
-
7/27/2019 2 - js
6/10
Event ObjectThe Event object represents the state of an event, such as
the element in which the event occurred, the state of the
keyboard keys, the location of the mouse, and the state of themouse buttons.
Event Handlers (Attributes)
onabort, onblur, onchange, onclick, ondblclick, onerror,
onfocus, onkeydown, onkeypress, onkeyup, onload,onmousedown, onmousemove, onmouseout, onmouseover,
onmouseup, onreset, onresize, onselect, onsubmit, onunload
-
7/27/2019 2 - js
7/10
JavaScript Exception Handling The try...catch Statement:
The try...catch statement allows you to test a block of
code for errors. The try block contains the code to be run,and the catch block contains the code to be executed ifan error occurs.
try
{
//Run some code here
}
catch(err)
{
//Handle errors here}
-
7/27/2019 2 - js
8/10
JavaScript Throw Statement
The throw statement allows you to create an
exception. If you use this statement togetherwith the try...catch statement, you can control
program flow and generate accurate error
messages.
Syntax:throw(exception)
ht l
-
7/27/2019 2 - js
9/10
var x=prompt("Enter a number between 0 and 10:","");
try {
if(x>10) {
throw "Err1";
}else if(x
-
7/27/2019 2 - js
10/10
JavaScript Special Characters The backslash (\) is used to insert apostrophes, new
lines, quotes, and other special characters into a text
string. Code Outputs
\' single quote
\" double quote
\& ampersand
\\ backslash
\n new line
\r carriage return
\t tab
\b backspace\f form feed