Site hosted by Angelfire.com: Build your free website today!


Select from the Menu Items Below

Slide Show

Wow! Look at everything you have learned so far. So far you have learned how to generate a JavaScript to automatically execute. Now we will learn how to generate a JavaScript that will execute on your users command. We will use the code from the last exercises, Cycling banner and Random banner, and modify it to create a slide show that the user can click a Next link to advance to the next images and a back link to go to the previous image. You will need to remove the select() function from the Random banner script, and we will be replacing it with 2 functions, doBack(), and doNext(). When you have completed, your functions should look like this:

function doBack()
{
  if(index > 0)
  {
    index--;
    document.slideshow.src = imgArray[index].src;
   }
  return;
}

function doNext()
{
  if(index< 3)
  {
   index++;
   document.slideshow.src = imgArray[index].src;
  }
   return;
}

You then need to take out your onLoad statement in your <BODY> tag. We do not need to pre-load this script so it is not needed. Now lets modify your <IMG> tag:

<H1>JavaScript Slideshow</H1>
<P>
< IMG src="lions.gif name="slideshow" width=400 height=100>
</P>
<P>
<H3>
<A HREF=javascript:doBack>Back</A>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<A HREF=javascript:doNext>Next</A>
</H3>
</P>


Let's run it and see what happens, here is what it should look like.

Click Here for Text File.

Now, on your own, try to modify the code so that when the user reaches the last image clicking the Next link, it will start all over again. The same thing with the Back link, modify it so that is will start over with the last image after the user has reached the first image clicking the Back link.

The only new concept we learned with this exercise is the inline JavaScript. Notice our Anchor tag....Instead of linking to a page, they are actually calling the JavaScript functions doBack(), and doNext.

JavaScript Source File

A lot of JavaScript is incorporated into an HTML document, however you can also call it from a source file, which is usually designated with a .js extension. If the JavaScript code is you are writting is short and it will only be used in the one HTML document, the just write the code in the document. However, you are writting an extensive JavaScript, and/or it will be used in Multiple HTML documents, then you will want to write it in a .js source file, and call the JavaScript source file. First you need to know that you do not include HTML tags in a JavaScript source file, you only include JavaScript in this file. Another possible reason to use a source file instead of including your JavaScript in your document would be to hide your JavaScript code. With your code hidden, it makes it less likely that someone will steal your code. This is great for script that include passwords or other sensitive types of scripts. It also makes your HTML document look much neater and not as cluttered. Creating a source file also hides your JavaScript from incompatible browsers. If your HTML document contains JavaScript code, instead of calling an external JavaScript source file, an incompatible browser will display the code as if it were standard text.

You can include embedded JavaScript as well as JavaScript source files in the same HTML document. To do this however, you do need to include each JavaScript inside it's own <SCRIPT> tag, whether it is calling a source file, or embedded in your HTML document.

You will include the <SCRIPT>...</SCRIPT> tag pair in your HTML document, and place the call to the source file inside the tag utilizing the SRC attribute. For example, to load a JavaScript Source file that is located in the C:\javafiles\samplesourcefile.js you would write the code as:

<SCRIPT LANGUAGE="JavaScript1.2"
SRC="c:\javafiles\samplesourcefile.js"></SCRIPT>

Let's do an example:

First you will need to create a JavaScript Source file, so open your text editor.

//This is my first JavaScript Source File
//I am only including one line of code
//This file is named javaScriptSource.js

document.write("This line was printed from the JavaScript source file.");

Now let's generate our HTML document:

<HTML>
<HEAD>
<TITLE>HTML Document with Two JavaScript Sections</TITLE>
</HEAD>
<BODY>
The following line calls an external JavaScript source file.<BR>

<SCRIPT LANGUAGE="JavaScript"
SRC="c:\javafiles\samplesourcefile.js"></SCRIPT>

<SCRIPT LANGUAGE="JavaScript">
document.writeln("This line was created with embedded JavaScript code.");
document.writeln("This line was also crated with embedded JavaScript code.");
</SCRIPT>

</BODY>
</HTML>


And the results are, Click Here to find out.

To be continued.......JS book p.33

 

Previous | Next

JavaScript Attributes

Back to Top

Designed by: