spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / prof_java2 / 1 To page 1current pageTo page 3To page 4To page 5
[previous][next]

Developer-Building Trading-Pricing Appl-Capital Markets C#-WPF--WCF-XML-.Net 3.5,ASP, SQL Server
WSI Nationwide, Inc.
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Google Going Native With Chrome
Mozilla Fixes Firefox Flaws as 3.5 Release Nears
Microsoft and Novell Still Bosom Buddies


Professional JavaScript for Web Developers: JavaScript in the Browser, Pt. 2

The status bar

The status bar is the area in the bottom border that displays information to the user (see Figure 5-9).

Normally, the status bar tells the user when the page is loading and when it has finished loading; however, it is possible to set its value using two properties of the window object: status and defaultStatus. As you may have guessed, status changes the status bar text for a moment while defaultStatus changes it as long as the user is on the page. For example, you may use a default status bar message when the page first loads:

You may also want to display information about a certain link when the user moves the mouse over it:

This is especially useful when using a JavaScript URL because browsers, by default, display the value of the href attribute in the status bar when the user mouses over. Setting window.status can keep the details of the link implementation from users:

Be careful not to overuse the status bar and thereby making it a distraction. For example, many sites still use the scrolling message code that scrolls text across the status bar. Not only is this a fairly useless trick, it’s also annoying, very unprofessional, and adds a very amateurish feeling that a Web site or Web application can do without. Because this book is all about professional JavaScript, the scrolling text code will not be covered. However, if you are interested in playing around with scrolling text, check out http://javascript.internet.com/scrolls/, where you can find a large number of these scripts.

Intervals and timeouts

Java developers are familiar with the wait() method of objects, which causes the program to stop and wait a specified amount of time before continuing on to the next line of code. This is a very useful piece of functionality and, unfortunately, one that JavaScript doesn’t support. But all is not lost. You have a couple of ways around this issue.

JavaScript supports timeouts and intervals, which effectively tell the browser when certain code should be executed: Timeouts execute the given code after a specified number of milliseconds; intervals execute the given code repeatedly, waiting a specified number of milliseconds in between.

Setting a timeout is done using the window’s setTimeout() method. This method accepts two arguments: the code to execute and the number of milliseconds (1/1000 of a second) to wait before executing it. The first argument can either be a string of code (as would be used with the eval() function) or a pointer to a function. For example, both these lines display an alert after one second:

Of course, you can also reference a previously defined function:

When you call setTimeout(), it creates a numeric timeout ID, which is similar to a process ID in an operating system. The timeout ID is essentially an identifier for the delayed process should you decide, after calling setTimeout(), that the code shouldn’t be executed. To cancel a pending timeout, use the clearTimeout() method and pass in the timeout ID:

You may be thinking to yourself, “Why would I define a timeout and then cancel it before it executes?” Consider the tooltips available in most applications today. When you move your mouse over a button, it takes a little bit of time before that friendly yellow box appears to tell you what the button does. If you move your mouse over the button for just a short time and then move it to another button, the tooltip isn’t displayed. This is precisely why you would cancel a timeout before the code executes: Because you want to wait a specified amount of time before the code is executed. If the user does something that would result in a different outcome, you need the flexibility to cancel that timeout.

Intervals work in much the same way except that they repeat the given code indefinitely at specific time intervals. To set up an interval, use the setInterval() method with the same type of arguments as you use with setTimeout() — the code to execute and the number of milliseconds to wait between each execution. For example:

Also similar to timeouts, the setInterval() method creates an interval ID to identify the code to be executed. It can be used with the clearInterval() method to prevent any further executions. Obviously, this is much more important when using intervals because, if left unchecked, they continue to execute until the page is unloaded. Here is a common example of interval usage:

In this code, the number iNum is incremented every 500 milliseconds until it reaches the maximum (iMax), at which point the interval is cleared. You can use timeouts for this, eliminating the need to keep track of a timeout ID, by doing the following:

Here, the code uses linked timeouts, meaning that the code being executed by setTimeout() also calls setTimeout(). If iNum is still not equal to iMax after it is incremented, another setTimeout() call is made. You don’t have to keep track of the timeout ID or clear it because after the code is executed, the timeout ID is destroyed.

So which method should you use? It’s really depends on the use case. To wait a certain amount of time before executing a certain set of code, use timeouts. If, however, you need some code to be executed repeatedly, then use intervals.

History

It is possible to access the history of a browser window. The history is the list of places the user has been. For security reasons, all you can do is navigate through the history; there is no way to get the URLs of the pages contained in the browser history.

To navigate through history, you don’t need a time machine; you use the window object’s history property and its associated methods.

The go() method takes only one parameter: the number of pages to go back or forward. If the number is negative, you are going backwards through the browser history. If the number is positive you are going forward (think of it as the difference between the Back and Forward buttons).

So, to go back one page, the following code can be used:

Of course, the reference to the window object isn’t necessary, so this will do:

Most often, this is used to create a custom “Back” button embedded in a Web page, such as:

To go forward one page, just use a positive one:

you can use the back() and forward() methods to accomplish the same thing:

These may be a little more meaningful because they accurately reflect the behavior of the browser Back and Forward buttons.

Although it’s not possible to see the URLs in the browser history, you can see how many pages are in it by using the length property:

This capability is helpful if you want to go back or forward by more than one page and want to know if that is possible.

home / programming / prof_java2 / 1 To page 1current pageTo page 3To page 4To page 5
[previous][next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
XML and PHP Simplified · Creating a ASP.NET Contact Form · Data Filtering with PHP
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Intel to Host Live Nehalem Q&A · 12 Tips to Troubleshoot Network File-Sharing · 10 Tips for Selling on Kijiji

Created: March 27, 2003
Revised: June 27, 2005

URL: http://webreference.com/programming/prof_java2/1