Make your own free website on
LinkExchange Member

Some beginning JavaScript

Here is some basic JavaScript to liven up your page.

JavaScript is a completely different than HTML. If you make mistakes in your scripts the whole script will probably fail to run and may cause other problems. I am on webtv wich is not capable of all JavaScript so my help will be limited to what I have gotten to work.

The first thing you must know is where to place it in your page.

It is not necessary but recommended because certain browsers require it to be right after the </title> and before the <head> tag.
Scripts will be placed through out a page on further pages. I have a script down by my guestbooks. Functions wich are described later on should always be up in the head.

Now for the actual beginning of the script.

<SCRIPT LANGUAGE="JavaScript"> I used upper case letters wich are not required. Also you could just put
<script> That's just in case you have a browser with multiple scripting languages. I believe this is rare for now so don't worry to much about that.
Now there is another part of the script that is not necessary for all browser, but because some browsers are not able to use JavaScript it is recommended
<!-- Hide from non JavaScript able browsers You could write whatever you want instead of
Hide from non JavaScript able browsers
You could write any word you want there. You don't want it to long or it could cause problems. I usually just put the word hide. If the browser is not capable of using JavaScript the page will have to have something telling them that they're missing something. I'll tell how to do this at a later time.(I don't remember right now).

The contents of the script goes next. For my first lesson. I'll make a simple alert or pop-up

alert("Welcome to my page"); This will cause a pop-up saying Welcome to my page Or you could put whatever you want between the qoutes. You'll have a semicolon ; after every command in a script.

Now you have to finish up the script before it will work. The next line will end the hide tag

// End Hide --> As you can see the beginning of the hide starts with <! and the end hide ends with >
This is the same way comments are added to HTML so non JavaScript browsers take the script as a comment. Like the comment tag for HTML pages.
<!-You can put these tags in any HTML page --> You'll notice those on alot of HTML pages or at the beginning of most counter or guestbook and LinkExchange codes. Their just for info for the code viewer. So a long one serves as a hide for a script. Wich would not be neccessary if all browsers were JavaScript able.

Now for the closing tag.
</SCRIPT> Now for the entire script. <SCRIPT LANGUAGE="JavaScript">
<! Hide from non JavaScript browsers
alert("Welcome to my page");
// End Script -->
You can also place some comments that would not be seen unless the source code is being viewed.
one line comments
// comments not longer than on line
comment blocks /* comments as long as you want
like credits. script written by
Permission to use if you
sell you soul etc

Making it more interactive

Now you know how to make a simple pop-up. Here is how you get the page to ask for some personal input

var name = prompt("What is your name?","Name here"); This will put up a prompt asking the person for there name. If this box were to pop up it would have the words name here already in the box. You could leave the space between the second set of qoutes blank if you don't want a default value already in the box.

The results of the prompt will be stored in the variable name You could use whatever word or letter you want in the place of name, except for certain words that are already used by JavaScript like alert or var and others. I'll put a link to the list of reserved words when I remember where I wrote the address at. A variable begins with a letter and can contain numbers and the underscore such as var viewer_name=prompt("What is your name","");. It is best to stick with variable names that help remind you what is stored in it. When you start writing longer scripts you'll want to be able to keep track of what is stored where.

Now how to use the value stored in name in your pop-up

Here is an addition to the alert statement

alert("Hello, "+name); I have placed the variable name within the alert box. The text of the alert is within qoutes, the variable is outside of the qoutes and must have the plus sign before it.
You could continue the text after the variable as follows
alert("Hello, "+name+" Welcome to my page"); You'll notice I made spaces within the qoutes otherwise you'll get Hello,BobWelcome to my page. Instead of Hello, Bob Welcom to my page
So now Here is the complete script for a prompt followed by a pop-up
<!-- Hide from non JavaScript browsers
var name = prompt("What is your name?","");
alert("Hello, "+name+" Welcome to my page");
// End script -->

If you want to control when a new line starts add this to your alert
alert("Hello, "+name+"\nWelcome to my page");
I put the \n after the second qoute so that right after the Hello Bob A new line would start. Welcome to my page instead of having the browser choose when to make the break wich can make your alert look funny
Try you hand at editing a short welcome script in. A sample script is below the editing window. Copy it to the window if you wish, and try changing the way the question and alert is worded. Try changing the place of the variable name and anything else you wish to try. If you like what you have made copy it and enter it into your page editor.

Lesson 1
Once you've got that down go on to
On to even more interactivity

Sign my GuestBook

View my GuestBook
Read my DreamBook

Sign my DreamBook

Quiz of the Week
An interactive weekly quiz. Some of wich I write myself

Post your questions or answers here

of the

|| index||page 1||page 2||page 3||page 4||page 5||page 6||page 7||Page 8||