ÄúµÄλÖãºÑ°ÃÎÍøÊ×Ò³£¾±à³ÌÀÖÔ°£¾JavaScript£¾Javascript Tutorial
Javascript Tutorial

Javascript - Intro
Javascript - Syntax
Javascript - Location
Javascript - External
Javascript - Operators
Javascript - Variables
Javascript - Functions
Javascript - Events
Javascript - Statements
Javascript - If
Javascript - Else If
Javascript - While
Javascript - For Loop
Javascript - Comments
Javascript - Array
Javascript - Alert
Javascript - Confirm
Javascript - Prompt
Javascript - Print
Javascript - Redirect
Javascript - Pop Up
Javascript - Date
Javascript - Form
Javascript - Void 0

Javascript String

Javascript - Strings
Javascript - Length
Javascript - Split
Javascript - Search
Javascript - Replace
Javascript - indexOf
Javascript - Compare

Javascript Advanced

Javascript - getElementById
Javascript - innerHTML

How To Write Javascript

If you have ever used CSS before, then the whole part about how to include Javascript will be a lot simpler to grasp. Here are Tizag's three important steps you should always take when creating or using someone else's Javascript code.

  1. Use the script tag to tell the browser you are using Javascript.
  2. Write or download some Javascript
  3. Test the script!

There are so many different things that can go wrong with a script, be it human error, browser compatability issues, or operating system differences. So whenever using Javascript, be sure that you test your script out on a wide variety of systems and most importantly, different web browsers.

Your First Javascript Script

To follow the classic examples of many programming tutorials let's use Javascript to print out "Hello World" to the browser. I know this isn't very interesting, but it will be a good way to explain all the overhead required to do something in Javascript.

HTML & JavaScript Code:

<html>
<body>
<script type="text/javascript">
<!--
document.write("Hello World!")
//-->
</script>
</body>
</html>

Display:

Our first step was to tell the browser we were using a script with <script> tag. Next we set the type of script equal to "text/javascript", which you may notice that doing this is similar to the way you specify CSS, which is "text/css".

Next we added an optional HTML comment that surrounds our Javascript code. If a browser does not support Javascript, then it will not display our code in plain text to the user! The comment was ended with a "//-->" because "//" signifies a comment in Javascript, so we add that to prevent a browser from reading the end of the HTML comment in as a piece of Javascript code.

Javascript document.write

The final step of our script was to use a function called document.write which writes a string into our HTML document. document.write can be used to write text, HTML, or a little of both. We passed the famous string of text to the function to spell out "Hello World!" which it printed to the screen.

Do not worry if you don't completely understand how document.write works, as we will be discussing functions in a later lesson.

Syntax

Looking at our Javascript code above, notice that there is no semicolon at the end of the statement "document.write(Hello World!)". Why? Because Javascript does not require that you use semicolons to signify the end of each statement.

If you are an experienced programmer and prefer to use semicolons, feel free to do so. Javascript will not malfunction from ending semicolons. The only time it is necessary to use a semicolon is when you choose to smash two statements onto one line(i.e. two document.write statements on one line).