Main Page

assign

The method
assign()
accomplishes the same thing:
location.assign(“http://www.wrox.com”);
Either way is fine, but most developers use
location.href
because it more accurately represents the
intent of the code.
If you don’t want the page containing the script to be accessible in the browser history, you can use the
replace()
method. This method does the same thing as
assign()
, but it takes the extra step of remov-
ing the page containing the script from history, making it inaccessible using the browser Back and
Forward buttons. Try it for yourself:
<html>
<head>
<title>You won’t be able to get back here</title>
</head>
<body>
<p>Enjoy this page for a second, because you won’t be coming back here.</p>
<script type=”text/javascript”>
setTimeout(function () {
location.replace(“http://www.wrox.com/”);
}, 1000);
</script>
</body>
</html>
Load this page in your browser, wait for it to navigate to the new page, and then try hitting the Back
button.
The location object also has a method called
reload()
that reloads the current page. The two modes for
reload()
reload from the browser cache or reload from the server. Which of these two modes is used
depends on the value of one argument:
false
to load from cache;
true
to load from the server (if the
argument is omitted, it is considered
false
).
So, to reload from the server, you use this code:
location.reload(true);
To reload from the cache, you can use either of these lines:
location.reload(false);
location.reload();
The final method of the
location
object is
toString()
, which simply returns the value of
location.href
. Therefore, the following two lines of code are equal:
Any code located after a
reload()
call may or may not be executed, depending on
factors such as network latency and system resources. For this reason, it is best to
have
reload()
as the last line of code.
154
Chapter 5
08_579088 ch05.qxd 3/28/05 11:37 AM Page 154


JavaScript EditorFree JavaScript Editor     Ajax Editor


©