Client-Side Scripting with JavaScript


Part 12 - JavaScript Tidbits

History and Location

The history object can access the history of URLs visited by the browser. This allows you to move back or forward one document or go to a particular document.

   window.history.back()

   window.history.forward()

   window.history.go()

   window.history.length

For example, click here to go back two documents.

<A HREF="javascript:window.history.go(-2)">click here</A>

Using window.history.length, we can tell that the number of documents you have viewed is:

The location object can be used to get information about the current URL or to change the document being displayed in the window.

Choose a part to go to:
The JavaScript for this is:

<SCRIPT LANGUAGE="JavaScript"> function ChangeLoc() { ysub = document.loc.part.selectedIndex; window.location = document.loc.part[ysub].value; } </SCRIPT> <FORM NAME="loc"> Choose a part to go to:<BR> <SELECT NAME="part" onChange="ChangeLoc()"> <OPTION VALUE="js3.html">3. Creating Documents with JavaScript <OPTION VALUE="js4.html">4. Language Features <OPTION VALUE="js9.html">9. Form Examples <OPTION VALUE="js10.html">10. Windows <OPTION VALUE="js11.html">11. Images </SELECT> </FORM>

The Timeout Method

JavaScript has a method that allows you to delay running a command or function:

   timerID = setTimeout(function, delay in milliseconds)

   clearTimeout(timerID)

This example uses the setTimeout method to scroll a list inside a form text box:

Department Abbreviations:

Some of the script:

<FORM NAME="scroller"> <TEXTAREA NAME="showtext" WRAP="VIRTUAL" ROWS=5 COLS=40> </TEXTAREA> <SCRIPT LANGUAGE="JavaScript"> var depts = new Array(49); depts[0]=" ACC - ACCOUNTING"; depts[1]=" ANT - ANTHROPOLOGY"; depts[48]=" WST - WOMEN'S STUDIES"; var startval = 0; var delay = 1000; var maxsub = 48; function ScrollText() { document.scroller.showtext.value = ""; for (looper=0; looper < 5; looper++) { if (startval > maxsub) {showline = " "} else {showline = depts[startval]} if (looper < 4) {showline += "\n"} document.scroller.showtext.value += showline; startval++; } startval = startval-4; if (startval > maxsub) {startval = 0} timerID = setTimeout("ScrollText()",delay); } ScrollText(); </SCRIPT> <INPUT TYPE="button" NAME="go" onClick="clearTimeout(timerID)" VALUE=" Stop Scrolling "> <INPUT TYPE="button" NAME="go" onClick="ScrollText()" VALUE=" Scroll ">

Next Back to the Index Page