P. 1
16103725 Advanced Javascript Second Edition

16103725 Advanced Javascript Second Edition

|Views: 155|Likes:
Published by Karthik Juvvala

More info:

Categories:Topics, Art & Design
Published by: Karthik Juvvala on Jul 15, 2010
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less






The location.reload method forces a reload of the window’s current document. Its
general syntax is:


Specifying the Boolean value true as the method’s optional argument forces an uncon-
ditional HTTP get of the document from the server. An unconditional get retrieves a
document directly from the server, ignoring the content of the browser’s cache, which
might already contain the desired data from that document. Therefore, true should not
be specified unless you have reason to believe that either disk or memory cache is
broken, or the server has a new version of the document. If such a version is available,
you must force an unconditional HTTP get because the version of the document stored
in cache is different from the new version on the server. This situation is common to
CGI-generated documents.

The reload() method simply reloads the document its URL stored in location.href.
It uses the same policy as the Reload or Refresh button. Microsoft has opted to label
the button “refresh” rather than “reload,” but will probably keep the same method
names. The exact reload process depends on the cache handling menu option. In
Netscape Navigator, the user sets the default value of this process by choosing Net-
work Preferences from the Options menu, and specifying Verify Documents on the
Cache tab of the Preferences dialog box.

310 n Chapter 17

The reload() method does not force a transaction with the server under normal con-
ditions. However, if the user has set the preference to “Every Time,” the request is an
unconditional get using an “if-modified-since” HTTP header. HTTP headers are
passed to the browser when a document is retrieved from the server. It contains
important information regarding the current document. If the user sets the preference
to “Every Time,” the browser checks the transmitted HTTP header to see if the docu-
ment has been updated according to the “last-modified time” property. If it has, the
document cannot be loaded from the cache which holds a previous version of the file.
In short, reload() will bring the cache’s version unless the user has specified “Every
Time” and the document has changed on the server since the last time it was loaded
and saved in the cache. Since its size is limited, the cache might lose a document ver-
sion that has not been reloaded for a long time. In this case, the document needs to be
fully loaded from the server, even if it has not been changed since the previous load.

In event handlers, you must specify window.location.reload() instead of simply
using location.reload(). Due to the static objects’ scoping in JavaScript, a call to
location without specifying an object name is equivalent to a call to document.loca-
tion, which is a synonym for document.URL. This concept is explained later in the
chapter in greater detail.

You have probably experienced situations in which you leave your computer connected
to a host and go out for a break, then come back to find that the connection has been
dumped. The usual cause is that the host (server) has disconnected you because you
have not transmitted any data via the server for a long time. You can overcome this
problem by periodically reloading a JavaScript document from the server. Next time
you go out for lunch, load the following document in the browser’s window:

stay connected

Example 17-3 (ex17-3.htm). A simple HTML document keeps the connection alive.

The onLoad event handler is used to call the reload() method. A setTimeout()
method delays the reload procedure for 200,000 milliseconds, or 200 seconds, from the
moment the document is completely loaded. Since it is used in the form of an event
handler, the reload() method must be fully specified, including the window object ref-
erence. The true argument forces the transaction with the server.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->