18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5<br />

The method assign() accomplishes the same thing:<br />

location.assign(“http://www.wrox.com”);<br />

Either way is fine, but most developers use location.href because it more accurately represents the<br />

intent of the code.<br />

If you don’t want the page containing the script to be accessible in the browser history, you can use the<br />

replace() method. This method does the same thing as assign(), but it takes the extra step of removing<br />

the page containing the script from history, making it inaccessible using the browser Back and<br />

<strong>For</strong>ward buttons. Try it for yourself:<br />

<br />

<br />

You won’t be able to get back here<br />

<br />

<br />

Enjoy this page for a second, because you won’t be coming back here.<br />

<br />

setTimeout(function () {<br />

location.replace(“http://www.wrox.com/”);<br />

}, 1000);<br />

<br />

<br />

<br />

Load this page in your browser, wait for it to navigate to the new page, and then try hitting the Back<br />

button.<br />

The location object also has a method called reload() that reloads the current page. The two modes for<br />

reload() reload from the browser cache or reload from the server. Which of these two modes is used<br />

depends on the value of one argument: false to load from cache; true to load from the server (if the<br />

argument is omitted, it is considered false).<br />

So, to reload from the server, you use this code:<br />

location.reload(true);<br />

To reload from the cache, you can use either of these lines:<br />

location.reload(false);<br />

location.reload();<br />

Any code located after a reload() call may or may not be executed, depending on<br />

factors such as network latency and system resources. <strong>For</strong> this reason, it is best to<br />

have reload() as the last line of code.<br />

The final method of the location object is toString(), which simply returns the value of<br />

location.href. Therefore, the following two lines of code are equal:<br />

154

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!