Professional Documents
Culture Documents
Ok, how are we doing on time? We have a lot to cover today and I don't want to
keep you here too late. So let me look at my clock and see what time it is
8:32:18 PM . Oh, it is only a little after 8:25, so we still have plenty of
time to get into an Introduction of JavaScript.
What is JavaScript?
I have already shown you that JavaScript can be used to display a specific
greeting based on the time of day and can be used to generate a simple clock.
ase take your time with each lesson and enjoy..... Welcome to Learning JavaScript for Be
It c a n p ut a d ro p d o wn me nu o n y o ur p a g e :
JavaScripts
This menu has been disabled but you will will learn how to do the real thing is this class.
It c a n d o p o p -up wind o ws :
It is fa s t:
javascriptmall.com/learn/lesson2.htm 1/9
10/11/2009 Learning JavaScript - Writing and Testi…
JavaScript is 100% text, just like HTML is. Therefore it loads
quickly from the server to your computer. JavaScript runs on your
computer and does not have to download any additional
applications to do it's job.
When you make a mistake and try to trouble shoot. But so can
HTML! Have you ever left off a closing tag such as </FONT> and
spent some time trying to find the mistake among the many tag
combinations on your page.
It is Ob je c t Orie nte d :
It is no t J a v a :
javascriptmall.com/learn/lesson2.htm 2/9
10/11/2009 Learning JavaScript - Writing and Testi…
browsers.
<HTML>
<HEAD>
<TITLE>Our First Script</TITLE>
</HEAD>
<BODY>
<CENTER>
<H1>Our First Script</H1>
</CENTER>
<P>Today is
</BODY>
</HTML>
Save this code as script1.html. It looks something like this when we load it into
our browser by using File/Open on the menu.
Not very exciting! Lets add some JavaScript to our code. Add the following
script after the line <P>Today is
<SCRIPT Language="JavaScript">
var today = new Date()
document.write(today)
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Our First Script</TITLE>
</HEAD>
<BODY>
<CENTER>
<H1>Our First Script</H1>
</CENTER>
<P>Today is
<SCRIPT Language="JavaScript">
var today = new Date()
document.write(today)
</SCRIPT>
</BODY>
javascriptmall.com/learn/lesson2.htm 3/9
10/11/2009 Learning JavaScript - Writing and Testi…
</HTML>
Save this script and reload it into you browser. It should render something like
this:
Still not real exciting, but remember this is only our first script and does
demonstrate something you can't do with plain HTML.
<TAG-NAME [ATTRIBUTE=["VALUE"]....]>
(some text)
</TAG-NAME>
Most browsers in use today have some version of JavaScript built in. However,
there are still a few in use that don't understand JavaScript. These browsers will
ignore the <SCRIPT> tag but will display the text between the tags. To prevent
this, the JavaScript code is enclosed by a combination of the HTML comment
tag <!-- --> and the double slash ( //) that is used in JavaScript to identify a
comment. The result looks like this <!-- //-->.
<SCRIPT language="JavaScript">
<!-- hide from old browsers
<SCRIPT language="JavaScript"><!--
Use whichever template that you feel the most comfortable with.
<HTML>
<HEAD>
<TITLE>Our First Script</TITLE>
</HEAD>
<BODY>
<CENTER>
<H1>Our First Script</H1>
</CENTER>
<P>Today is
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
var today = new Date()
document.write(today)
//-->
</SCRIPT>
</BODY>
</HTML>
javascriptmall.com/learn/lesson2.htm 5/9
10/11/2009 Learning JavaScript - Writing and Testi…
anywhere in the BODY section of HTML code. Now I am going to do something
daring and ask you to look at my HTML code for this page. Please be nice now
and don't find too many faults (LOL). Ok if you look in the head section, you will
notice some <SCRIPT> tags there too. So, yes you can put JavaScript in the
HEAD section also. Notice that the word func tio n is used several times in the
HEAD section. A function is something that we will learn about real soon. Just
wanted you to note these and get the idea that the HEAD section is where you
might put your functions. So it turns out that JavaScript can be placed most
anywhere in our HEAD and/or our BODY sections. As you learn more about
JavaScript, you will understand when you should put it in the HEAD section and
when it should go in the BODY of the document. You will also learn that
JavaScript can also be placed inside other tags, such as the <BODY> tag.
Here is a sample of how the error will look on the Netscape and Internet
Explorer browsers. Yours may vary a little from this.
Netscape 4
(click on image for larger view)
javascriptmall.com/learn/lesson2.htm 6/9
10/11/2009 Learning JavaScript - Writing and Testi…
Notice that all of the error messages show a line number that the error occurred
on. In our simple case the line number is exactly where the error occurred. This
will not always be the case as our scripts get more complex. However, it will
javascriptmall.com/learn/lesson2.htm 7/9
10/11/2009 Learning JavaScript - Writing and Testi…
give you some clue as to where to start looking. The error call out can be a little
confusing and needs some getting use to.
You probably want to change the case for "document.write" back to all small
letters in our example script, save it and make sure it runs ok before we proceed
further.
document.write("string")
So, what is a string? As we will learn in lesson 4, it is nothing more than a series
of characters. You put the characters in quote marks, except when the string is
something that is returned by another method, which was the case in our first
script, or is a variable which you will also learn about in lesson 4.
You can include HTML tags in this string if you want. For instance, we can make
our welcome bold by doing the following. I will add a second line too.
<IMG src="pet.gif">
We will need to put quotes around this when we use it with the
document.write() method. This will mean that we will have quotes
within quotes. This will not work but there is a solution. Yo u a lte rna te ly us e
s ing le q uo te s a nd d o ub le q uo te s in J a v a S c rip t if y o u ne e d to p ut
q uo te s within q uo te s . Heres how we would do our image:
document.write('<IMG src="pet.gif">')
or
document.write("<IMG src='pet.gif'>")
Ok, so it is true that we can do this much simpler by just typing our words into the
document itself without all this document.write() stuff. After we learn a little
javascriptmall.com/learn/lesson2.htm 8/9
10/11/2009 Learning JavaScript - Writing and Testi…
more JavaScript we will be using this method to dynamically create sections of
a web page which is actually what we did with our first script. In fact you can
create the complete document for a web page using
document.write().
Don't forget that you must put our above example scripts between the
<SCRIPT> </SCRIPT> tags for them to work.
Confused yet?
So are you completely confused yet? If you can
Then I would say you have learned a great deal in this lesson and are ready to
move on and learn the basics on JavaScript.
Assignment
1. Surf the web and try to determine where JavaScript was used. Note
pages that used JavaScript in an effective way.
javascriptmall.com/learn/lesson2.htm 9/9