Beginner JS Pt. 1

Beginner JS Pt. 1Topics:

  • Calling the Script
  • Writing Comments
  • Doing Stuff (Basic)

Calling the Script

First of all you have to call the script. You do this by puting the <script> tag into your HTML document.

<script type="text/javascript">

That is the correct way to call up a JS script. However, in this age of foolproof browsers, most coders don't bother puting the whole tag in. Some will just use <script>, which when put through the W3C HTML validator will throw up an error.

So we've called the script. What next? Well, the next bit is optional. If you want, you can incase the whole script in HTML comments. The idea behind this is that any browser smart enough to read JS knows to ignore HTML comments in <script> tags.

<script type="text/javascript">
<!--
'Some JS goes here'
//-->
</script>

The <!-- and //--> parts are the HTML comments, which tells older browsers not to execute the code.

Comments

JS comments are used to describe the code-they often contain copyrights or instructions. There are two different types

//

and

/**/

// Will tell JS to disregared everything on the rest of the line.

/*Will tell JS to disregared everything until */

This is how you would use comments;

<script type="text/javascript">
//This is a single line comment
/*This is a multi line comment*/
</script>

<script type="text/javascript">
//This is right

//this is wrong

/so is this/

/*but not this*/

/*or this*/

</script>

Doing Stuff

Being able to call scripts is all fine and well, but what use is it if you can't do anything? Here we are going to cover three basic built in functions of JS, document.write, alert and location.replace. In case you're wondering, functions do things. There are quite a few built into JS, and you can write your own, but we'll get into that later.

document.write

This little baby basicaly writes some text onto the page. Neat! This is how we would use it;

<script type="text/javascript">
document.write('blah')
</script>

You can see I've left out all of the fancy markings and stuff.

This will put the text blah onto your screen. The apostrophies are important because they tell JS that the value in the brackets is a string and not a variable (something we'll discuss another time).

alert

The alert function makes a pop up on your screen;

<script type="text/javascript">
alert('blah')
</script>

This will create a popup box with the word blah in it.

location.replace

This will replace the current URL of your browser window with whatever is specifed.

<script type="text/javascript">
location.replace('http://www.google.com')
</script>

What will this do? It will forward the user to the aforementioned website.

Putting it all together

<script type="text/javascript">
<!--
/* A Cool Code */
// By The Gunny
document.write('This site sucks');
alert('So the magic of JavaScript is sending you to a better site!');
location.replace('http://www.google.com/');
//-->
</script>

Nice eh? You'll notice that I've put ; at the end of each line. This tells the browser that the line is finished-go to the next one. Some lines need them, others don't. They're a bit like braces, which we will discuss later and it becomes a bit hit and miss placing them. I'll talk more about this when we talk about 'if'.


Close    To Top
  • Prev Article-Web Design:
  • Next Article-Web Design:
  • Now: Tutorial for Web and Software Design > Web Design > JavaScript > Web Design Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction