14.08.2016 Views

Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7 ■ <strong>JavaScript</strong> <strong>and</strong> User Interaction: Navigation <strong>and</strong> Forms<br />

Using replace() can be quite annoying, because you break the user’s Back button functionality. She cannot go<br />

back to the current page when she doesn’t like the one you sent her to.<br />

The list of pages the user visited before reaching the current one is stored in the window.history object. This<br />

object has only one single property, called length, which stores the number of already visited pages. It has a few<br />

methods you can use (<strong>with</strong> the last two in the following list added as part of HTML5):<br />

• back(): Go back one page in the browser history.<br />

• forward(): Go forward one page in the browser history.<br />

• go(n): Go n steps forward or back in the browser history depending on whether n is positive or<br />

negative. You can also reload the same page via history.go(0).<br />

• pushState (state, title, url): This method pushes data into the session history, where<br />

state represents an object full of data, title represents the page title, <strong>and</strong> url represents the<br />

URL added to the history.<br />

• replaceState (state, title, url): This method works in the same way pushState() does,<br />

but it modifies data instead of adding new data to the browser’s history.<br />

The history object allows you only to navigate to the other pages—not read out their URIs or change them.<br />

The exception to the rule is the current page, which gets wiped from the browser history when you use replace().<br />

As already explained in the previous chapter, you effectively simulate browser functionality by sending the user<br />

to other pages via <strong>JavaScript</strong>, which might be superfluous or thoroughly confuse the user.<br />

In-Page Navigation<br />

You can use <strong>JavaScript</strong> to make navigation <strong>with</strong>in the same page more interesting <strong>and</strong> less screen-space consuming.<br />

In HTML, you can provide in-page navigation via anchors <strong>and</strong> targets, both defined <strong>with</strong> the tag, either <strong>with</strong> an<br />

href attribute for an anchor or a name or id attribute for a target.<br />

■ ■Note The name attribute for anchors is deprecated in HTML5, <strong>and</strong> it actually is enough to provide an ID to link an<br />

anchor <strong>and</strong> target pair. However, to ensure compatibility <strong>with</strong> older browsers, it might be a good idea to use it as you will<br />

in the following example.<br />

Let’s take a list of internal links in a table of contents linked to different targets further down the page as an<br />

example for in-page navigation:<br />

exampleLinkedAnchors.html (excerpt)<br />

X - a tool that does Y<br />

<br />

<br />

Information<br />

Demo<br />

Installation<br />

Use<br />

License<br />

Download<br />

<br />

<br />

www.it-ebooks.info<br />

203

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

Saved successfully!

Ooh no, something went wrong!