Title: JavaScript is an object-based language
1Introduction
- JavaScript is an object-based language
- Uses built-in objects
- Properties are attributes of objects
- Methods are actions performed on an object
2(No Transcript)
3Introduction
- An event is the result of a users action
- Event handlers are the way to associate that
action with the set of JavaScript codes you want
executed
4Inserting ltSCRIPTgt Tags on a Web Page
- JavaScript code can go anywhere in HTML
- Place all JavaScript code between the ltSCRIPTgt
and lt/SCRIPTgt tags - Set the LANGUAGE attribute so the browser knows
how to interpret your code - The HTML comment line hides any script language
the browser might not be able to interpret - ltSCRIPT LANGUAGEJAVASCRIPTgt
- lt! Hide from old browsers
5Entering the Start ltSCRIPTgt Tag and Comment
- Click the blank line (line 10) above the lt/BODYgt
tag
6Type ltSCRIPT LANGUAGE JAVASCRIPTgt and then
pressthe ENTER key. Type lt! Hidefrom old
browsers and thenpress the ENTER key
7(No Transcript)
8Using a Flicker on a Web Page
- Changes the background color in rapid succession
- JavaScript allows you to set the background color
multiple times, while HTML only allows you to set
it once (in the BODY tag)
9Setting the Background Color to a Static Color
- The last color you specify will be the browsers
final background color - With the insertion point on line 18, type
document.bgColorblanchedalmond and then press
the ENTER key
10Completing the JavaScript Selection
- It is now necessary to end the comment tag and
close the ltSCRIPTgt tag - If necessary, click line 19. Press the ENTER key
to create another blank line - With the insertion point on line 20, type //-gt
and then press the ENTER key. Type lt/SCRIPTgt and
then press the ENTER key
11(No Transcript)
12JavaScript Variables
- Used to store values temporarily
- Global variables
- Local variables
- Variables are considered global, except if they
are declared in a user-defined function, in which
case they are local
13JavaScript Variables
14JavaScript Variables
- JavaScript variables are loosely typed
- You are not required to define the data type
- JavaScript defines the data type for you
- String data types
- var LastName Simon
- Numeric data types
- var pageCnt 1
- Boolean data types
- var Done false
15Extracting the System Date
- Built-in Date() object
- Can be manipulated only by creating a new object
instance - var variable new object
- var birthDay Date(Jul, 13, 1975)
- Returns July 13, 1975 to birthDay
- var curDate new Date()
- Returns the current system date and time
information as an object instance
16Extracting the System Date
- The variable curDate cannot be manipulated
- To extract the date, convert curDate to a string
using the toLocaleString() and substring methods
17(No Transcript)
18Extracting the System Date
- Define a new object for the date in MM/DD/YY
HHMMSS format - var tNow new Date()
- Extract the date and time and store it in
MM/DD/YY HHMMSS format - var tlocDate tNow.toLocaleString()
- Extract only the date portion from the tlocDate
variable using relative addressing - var tDate tlocDate.substring(0,10)
19Extracting the System Date
20Extracting the Current System Date Using the
Date() Object
- Click the Notepad button on the taskbar to
activate the Notepad window. Click line 19 below
the document.bgColorblanchedalmond statement
21Type var tNow new Date() and thenpress the
ENTER key. Typevar tlocDate
tNow.toLocaleString()and then press the ENTER
key. Typevar tDate tlocDate.substring(0,10)an
d then press the ENTER key
22(No Transcript)
23Displaying the Current System Date
- Use plus signs () to concatenate (join) strings
and variables
24Displaying the Current System Date in the Initial
Greeting
- Click line 22. Press the SPACEBAR four times.
Type document.write(ltH2gtltCENTERgtWelcome, today
is tDatelt/CENTERgtlt/H2gt) and then press the
ENTER key
25(No Transcript)
26Constructing a Message Using Several Variables
- Click line 23. Type var intro1 Hi, thanks for
visiting our Web site, but we have moved. Please
make a note and then press the ENTER key - Type var intro2 of our new URL
(www.funphonics.com) and notify the Webmaster
about our new and then press the ENTER key
27Constructing a MessageUsing Several Variables
- Type var intro3 location. ClickltA
Hrefhttp//www.scsite.com/gtherelt/Agt or wait 15
seconds and then press the ENTER key - Type var intro4 to be moved automatically to
our new site. and then press the ENTER key
28Constructing a MessageUsing Several Variables
- Type var introMsg intro1intro2intro3intro4
and then press the ENTER key
29(No Transcript)
30Writing the Message on the Web Page
- Click line 28. Press the SPACEBAR four times.
Type document.write(ltH4gtltFONT Colorfirebrickgt
introMsglt/H4gtlt/FONTgt) and then press the
ENTER key
31(No Transcript)
32Calling a JavaScript Function
- Two basic methods to call a function
- Event handlers and object methods
- Code the function name in a JavaScript section at
the logical point of execution - setTimeout() method
- Causes a delay before an instruction is executed
33Calling a JavaScript Function
34Writing the setTimeout() Method to Execute the
chngSite() Function
- If necessary, click line 29. Press the SPACEBAR
four times - Type setTimeout(chngSite(), 15000) and then
press the ENTER key
35(No Transcript)
36Displaying the Last Modified Document Date
- If necessary, click line 30. Press the SPACEBAR
four times - Type document.write(ltBRgtltH4gtltCENTERgtThis
document was last modified document.lastModified
lt/CENTERgtlt/H4gt) and then press the ENTER key
37(No Transcript)
38Writing a JavaScript User-Defined Function
- A function is JavaScript code written to perform
certain tasks repeatedly - Built-in functions
39Writing a JavaScript User-Defined Function
- User-defined functions
- Written by the Web developer
- Functions can have data passed to it
- To call a function means to have JavaScript
execute the function
40(No Transcript)
41(No Transcript)
42Entering the chngSite() User-Defined Function in
the HEAD Section
- Click the blank line (line 4) between the end
lt/TITLEgt tag and the end lt/HEADgt tag
43Type ltSCRIPT LANGUAGEJAVASCRIPTgtand then
press the ENTER key. Typelt!Hide from old
browsers and thenpress the ENTER key
44Entering the chngSite() User-Defined Function in
the HEAD Section
- Press the SPACEBAR four times. Type function
chngSite() and then press the ENTER key. Press
the SPACEBAR eight times. Type alert(You are
about to be transported to the new site
location!) and then press the ENTER key. Press
the SPACEBAR eight times. Type location
http//www.scsite.com/ and then press the ENTER
key. Press the SPACEBAR four times. Type and
then press the ENTER key. Type //-gt and then
press the ENTER key. Type lt/SCRIPTgt and then
press the ENTER key
45(No Transcript)
46Saving the File on the Floppy Disk
- With the HTML Data Disk in drive A, click File on
the menu bar - Click Save on the File menu
47Testing the Web Page
- Click the Fun with Phonics button on the taskbar
- Click the Refresh button on the browser toolbar
- If you are connected to the Internet, click the
OK button when the dialog box displays with the
alert message - Click the Back button on the browser toolbar to
return to the Fun with Phonics Web page
48(No Transcript)
49(No Transcript)
50Printing the HTML File Using Notepad
- If necessary, click the Notepad button on the
taskbar. Click File on the menu bar and then
point to Print
51Click Print
52(No Transcript)
53Closing Notepad and Your Browser
- Click the Close button on the browser title bar
- Click the Close button on the Notepad window
title bar
54Summary
- Discuss how to integrate JavaScript with HTML
- Insert ltSCRIPTgt tags on a Web page
- Write start and end ltSCRIPTgt tags
- Define and use flickering to draw attention
- Describe the background color property of the
document object
55Summary
- Set the background color of a Web page using
JavaScript - Save the HTML file
- Test the Web page
- Discuss JavaScript variables
- Extract the system date
- Use several variables to construct a message
56Summary
- Describe the write() method of the document
object - Write a user-defined function that displays a
message and links visitors to a new Web site - Describe how the setTimeout() method works
- Use the lastModified property to display the last
modified document date - Print an HTML Notepad file
57What You Should Know
58Project 8 Complete