Thursday, April 18, 2024

HTML Goodies: Script Tip: Week 9

You there, Tippy!

We’re slowly getting to the bottom of this script. Now we’ll get into the history statement right at the end and wrap it up. Here’s the script so you can familiarize yourself again:







<SCRIPT LANGUAGE=”javascript”>


function JemWebCONFIRM()

{if (!confirm (“YOUR MESSAGE GOES HERE”))
history.go(-1);return ” “}


</SCRIPT>





If you’ve used any of the 3- or 4-level Navigator or Explorer browsers, then you’ve probably already seen this “history” in action. You go up to the location bar at the top of the browser and type in part of a URL and poof! The remainder of the address just pops up.

But how does the browser know that? It knows it because every place you go, in the order you go, is kept in a file called “history.” So, when you start to type in a URL, if you type in something that’s similar to an address that’s already in the history folder, the browser attempts to be nice and finish it off for you.

What’s nice about that history file is that JavaScript sees it as an object to be acted upon. In the case of this script, the method that acts upon it is “go.” That tells the browser to, well, go to the URL.

Notice in this script that not only is the browser being told to go somewhere within the history file, it is being told to go somewhere specific. In this case it is to go one back. See that (-1)?

Well, if that’s the case, then you should be able to tell the browser to jump three back, or four ahead (by using the plus sign instead of the minus). You can. As long as the user has surfed enough to have that many pages in their history file, it’ll work just fine. The problem comes in when you say to jump back four pages and the user has only visited three. The JavaScript cannot complete the command and it all comes to a standstill.

And That’s That

So, now you’ve seen another script explained note for note. Look at it again and see if you are not now starting to see the process of JavaScript. There is a real order to things that you have to understand before you can start writing your own scripts. Here, again, are the steps in the script:


  • You log into the page and the script is loaded into the browser.
  • The function is enacted upon the loading of the page.
  • A confirm box pops up with text written on it.
  • If the person chooses OK, the script is done and the page loads.
  • If the person chooses Cancel, then the history.go is enacted and the browser goes one page back or reloads the page the user just came from.

Next Week: A New Script! Browser Choice



     Do YOU have a Script Tip you’d like to share? How about suggesting a Script Tip to write about? I’d love to hear it. Write me at: jburns@htmlgoodies.com.



Learn to write your own JavaScripts with the
JavaScript Goodies!



You can find many other uses for JavaScript
on your Web pages here!

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured