
Lesson 1-Introduction
Welcome to JavaScript Kit's Interactive JavaScript Book. This online book will teach you everything you need to truly understand JavaScript, as you will witness for yourself soon.
If you are content with using simply html for creating webpages, you are ready to head out to section15 , how to make fire with sticks; the rest of us, lets rock.
One final word-there is quite a lot to absorb in this book if you're new to JavaScript...I'd like to offer a sincere advice, and that is to take it slowly. Go through each lesson carefully, and actually try out the examples yourself. No one says you have to read through all 27 lessons in one day, although a week is more than reasonable!
In this section, we'll be looking at the following topics:
Introduction to
JavaScript: FAQs about this language.
What is JavaScript?
I sure hope this is simply a rhetorical question, but anyways: JavaScript is a scripting
language that runs solely on a JavaScript compatible browser, ie: Netscape and IE. It
differs from most other programming languages in that it is relatively easy to master,
even for people who have absolutely no programming experiences whatsoever. It's main
function is to enhance webpages, although it has grown as NS4.0 and IE4.0 shipped to a
more powerful tool capable of creating full blown applications.
Why learn JavaScript?
The first few words that come to mind are: "Freedom baby, freedom!" With html,
you are restricted to creating static, non interactive webpages. This, in today's internet
standards, is unacceptable. With JavaScript, you can change that. Imagine being able to
break free and allow your creativity to dictate what you put on your webpage, instead of
the other way round. And the best part is, JavaScript can be learned by anyone-yes, I said
anyone!
What's the difference between Java and JavaScript?
Java is completely different from JavaScript-It's a lot more powerful, more complex, and
unfortunately, a lot harder to master. It belongs in the same league as C, C++, and other
more complex languages. Also, you need to compile a Java program before you can run it,
whereas with JavaScript, no compilation is needed-simply open up a text editor, type it,
save it, and your browser is ready to run it!
Can my JavaScript programs run on both Netscape and Internet Explorer browsers?
Unfortunately, not necessarily. JavaScript was created by Netscape, so it is most
compatible with Netscape. Internet Explorer 4.x supports 99% of what JavaScript has to
offer, although IE 3.x is not quite as adorable. A good rule to follow is to always test
your codes using both browsers before uploading it onto the internet. You will be
surprised how many websites fail to do this, annoying surfers and not even realizing that
their scripts are going haywire behind their backs! (this might pertain to me too)
Getting Started: Setting Up your code.
Where do your JavaScript codes go? Well, basically anywhere inside the <html> tags of your page. The beginning of your code begins with <script> and ends with </script>
<html>
<head><title>This is an example page</title></head>
<body>
Welcome to the JavaScript course!
<script language="JavaScript">
<!--
document.write("Hi there. This text is written using JavaScript!")
//-->
</script>
</body>
</html>
Output: Hi there. This text is written using JavaScript!
As you can see, we began our script with the tag <script language="JavaScript"> The part in orange is purely optional, but you should include them to remind yourself-and others that you are using JavaScript now. The second and next to last lines of the above example are <!-- and //-->, which are the comment tags. These tags should ALWAYS be included to help hide your code against older browsers of both Netscape and IE. If you don't include them, and someone is using an old browser, the browser will just "dump" all your code as text onto the screen, in other words, not a pretty sight! The only "functional part" of this script is the document.write(".......") part. It basically writes to the page whatever you put inside the quotation marks. Don't worry so much about why this is so yet, we will discuss this in detail later. We end this entire code with </script> This terminates your script, and brings you back to html.
Like html, you can insert comments in your JavaScript codes. Comments are ignored by the browser, and only used as reminder or documentation for your code. To basic syntax of inserting comments is either:
//
for single-lined comments, or
/*
.......*/
for multiple ones.
For example:
<script
language="JavaScript">
<!--
//this script does nothing and is useless!
/*Hay, don't involve me
in this!*/
//-->
</script>
Ok, we are now ready to proceed to some real programming!