HTML5: Managing Browser History

HTML5: Managing Browser History

with Bill Weinman

 


This course describes how to use the HTML5 Session History application programming interface to present bookmark-friendly URLs while updating parts of a page and preventing unnecessary network activity. Author Bill Weinman shows how to manipulate history by adding entries to the stack, visibly changing the URL in the address bar, and removing entries when the user presses the back or forward buttons, all without reloading the web page. The course uses practical examples that can be applied to most web sites, and also shows how to detect support for the History API in browsers and how to handle exceptions.
Topics include:
  • Manipulating browser history: an overview
  • Detecting History API support
  • Pushing a new URL onto the stack
  • Handling back and forward buttons with the popstate event
  • Updating the stack with replaceState()

show more

author
Bill Weinman
subject
Developer, Web, Web Development
software
HTML
level
Intermediate
duration
35m 33s
released
Aug 05, 2011

Share this course

Ready to join? subscribe


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:04Hi, I'm Bill Weinman, and I'd like to welcome you to HTML5:
00:08Managing Browser History.
00:10In this course, we'll look at the HTML5 session history application programming
00:15interface, including how to push history items onto the stack, how to modify the
00:20current history item, and how to use history events to update items on your page
00:25when a user presses the Back and Forward buttons.
00:28I'll also show you a complete working application that uses the HTML5 session
00:34history API to build a simple photo viewer, demonstrating real-world usage of
00:39the session history API and putting your knowledge to work.
00:42Learn how to use this new technology in HTML5:
00:46Managing Browser History.
Collapse this transcript
Using the exercise files
00:00If you have access to the exercise files for this course, you can use them to
00:04follow along with the lessons as I present them.
00:07In order to follow along, you'll need to upload these files to a web server,
00:12or install a web server on your working computer.
00:15The technology presented in this course will not work without a web server.
00:20Here in the ExerciseFiles folder you'll find a folder for the lessons in
00:24Chapter 2 of the tutorial.
00:27The files inside this folder are generally organized by movie number, and many
00:31have a -start and -done file as a beginning and ending place for that movie.
00:37You'll want to start with a working copy of the -start file and work with the
00:41copy, so that you can start over again with a fresh copy if you need to.
00:47In the CSS folder, you'll find a simple CSS file that is used for all of the examples.
00:52And in the Javascript folder, you'll find a small JavaScript library called
00:57bwH5.js that is used by many of the examples.
01:02These JavaScript and CSS files are covered in more detail in this chapter.
01:07If you do not have access to these exercise files, you can easily follow along
01:11and create files of your own.
Collapse this transcript
About the bwH5.js library
00:00The exercise files for this course includes a small JavaScript library called bwH5.js.
00:07This is used in a number of my HTML5 application technology courses to keep the
00:12examples simple and uncluttered to better focus on the topic you are learning.
00:16You'll notice at the top of this library there is a version number.
00:21Please use the version that came with the course that you're following.
00:24You don't need to use the latest version of this file,
00:28including a variable called element that's simply a reference to an
00:31anonymous function.
00:33This is used as an alias for document.getElementById, and it's really just a shortcut.
00:39There are functions for testing for some common HTML5 APIs,
00:44there is a function for displaying errors and a function for displaying status
00:49messages, and there is the bwTable class that's not actually used in this
00:54course but is used in some of my other courses to display tabular information.
01:01Feel free to copy this library and modify it for use in your own projects.
Collapse this transcript
About the CSS files
00:00The exercise files for this course includes a small CSS file called main.css.
00:06This same CSS file is used for many of my HTML5 application technology courses.
00:12Let's take a look at this file.
00:14This file is based on the colors from the Explore California web site
00:18that's used in some of the other HTML5 courses on the lynda.com Online Training Library.
00:25It starts with a reset for a lot of the common HTML elements.
00:30It sets some defaults for body, it has a class for the color red, classes for
00:36messages and errors, a class that sets up the content box, and sets some
00:42defaults for the header elements.
00:44bwTable is not used in this course but these are classes for formatting tables
00:49for displaying tabular information in some of my other courses.
00:54Finally, there are some defaults for form elements.
00:56Again, this is used in some of the other courses;
00:59it's not used in this course.
01:02As always, feel free to use this CSS file as a starting place for your
01:07own projects.
Collapse this transcript
1. Overview
Manipulating browser history
00:00The HTML5 session history API is a small set of objects and methods for
00:05manipulating the history stack in a browser.
00:08With this API you may add items to the browser history and change the location
00:12URL without initiating a new connection to the web server.
00:16This has great value for AJAX and other applications that may want to update a
00:21page without causing a complete page reload, while still providing bookmarkable
00:26URLs for your users.
00:28Officially, the spec is called the HTML5 session history and navigation API.
00:34It's a simple spec, and it's fairly stable.
00:37The implementations are a little inconsistent, but it's not difficult to manage.
00:42The HTML5 session history API requires a network connection;
00:46it will not work on a local file on your system.
00:49The reason for this is that it requires a host name, and it requires that the
00:54host name not be changed in any of the URLs that are pushed onto the stack, as
00:58this would be considered a security risk.
01:00So in order to follow along with the exercise files in this course, or even to
01:05work with the HTML5 session history API at all, you will need a web server.
01:11You cannot use this API with a file on your local system.
01:15If you don't have a web server, or you would like to work on a local machine,
01:19take a look at the course Installing Apache, MySQL, and PHP here in the
01:24lynda.com Online Training Library.
01:27The HTML5 session history API allows new items to be pushed onto the history
01:31stack, or the top of the stack maybe modified.
01:35When the Back button or Forward button is pressed, or the history.go method is
01:40called, an event is triggered and that event maybe captured to do local page
01:45updates without having to go back to the network.
01:47This provides significant opportunities for performance improvements.
01:52The API works in Chrome, Safari, Firefox, and most mobile browsers.
01:57Support is planned for upcoming versions of Opera, and it is not supported in
02:01current versions of Microsoft Internet Explorer.
02:05The HTML5 session history API provides a simple and usable interface for
02:11manipulating and reading the browser history stack.
Collapse this transcript
Viewing a sample application
00:00To demonstrate the HTML5 session history API, I've created a simple photo
00:05browser application.
00:06This is a simple implementation of a very common use of this feature.
00:11Let's take a look at how this works.
00:13I am using the Chrome web browser for most of the course.
00:16Of all the browsers that support the HTML5 session history API, Chrome is
00:21the most consistent.
00:22This application works fine in other browsers as well, as we'll see later in this movie.
00:27Unfortunately, Internet Explorer does not support the HTML5 session history API at all.
00:33You will also notice that this is running on my server.
00:37You will notice in the location bar at the top it says h5.bw.org.
00:42That's a temporary server that I've set up just for recording this course.
00:46Please do not try to run your lesson files on my server;
00:49that will not work.
00:50You will need your own server to run the lesson files.
00:53We'll look at the code in a lot more detail in the next chapter,
00:57but for now, let's just take a quick tour of how this works.
01:00You will notice that in the location bar, a file name part is Pager-Coffee.html,
01:07And so this is a photo browser--
01:08I am calling it also an Image Pager-- and you will notice that it is showing a cup
01:13of coffee and it has the caption that says Coffee.
01:16If I press the Left button here, you will see that it changes to Scissors and
01:22you will see also that the URL changes to say Pager-Scissors.html, the title
01:27bar changes up here, and yet, it's not going back out to the network to load another page.
01:33It's just changing the image here.
01:35If I press it again, we get another picture, this one a Paper.
01:38You see this message here, it says, "pushing Paper onto browser history," and I
01:43press it again, now it says, "pushing Rock onto browser history."
01:47If I press the Back button, we get back to the Paper which was the last one;
01:51press it again, we get back to the Scissors.
01:54You will notice, if I press the Forward button and hold it, we have
01:58several of them in here.
01:59I can skip forward to Paper, I can skip backwards to Coffee, and all of that
02:04happens and it changes the URL, but it does not go back out to the network to
02:08load the page again.
02:09We can see that this also works in other browsers. Here is Firefox. And we can
02:15go forward this time. We have Keyboard, we have a mouse. We'll go backward a few.
02:21It takes it a moment the first time it loads an image, but then it doesn't
02:24anymore, and you see that all the other information is updating as well. And if we
02:28hold the Back button here, we've got Coffee, hold the Forward button here, we've
02:32got Scissors, and all of that works exactly as we expected.
02:36It also works just fine in Safari.
02:39This is Safari here. And here's our Back button.
02:47We've got Coffee, we have Paper, Forward, Keyboard, and Mouse, and all of
02:53that works fine in Safari.
02:55If we load up Internet Explorer, we see we have this little alert box, it says,
02:59"This browser does not support HTML5 Browser History Manipulation."
03:03If I say OK, it goes ahead and loads the image, and it works just fine, but it
03:09does not update the URL, and it does not update the browser history, because it
03:15simply doesn't support that API.
03:16So I have a test in the code that tests whether or not the browser supports the
03:20spec and if it doesn't, then it just doesn't use those features, but it goes
03:25ahead and degrades gracefully.
03:28So a photo browser is a common use of the HTML5 session history API because
03:33it presents a common problem where an entire page maybe loaded just to update one item.
03:38Here we can see how this use case can be significantly improved by using the
03:43HTML5 session history API.
03:45In the rest of this course, we will look at the details of how this is
03:48implemented, so that you can understand how to create an application like
03:51this yourself.
Collapse this transcript
2. The Details
Detecting HTML5 History API support
00:00Detecting support for the HTML5 session history API is relatively simple.
00:05All we have to do is test for the presence of a couple of object methods.
00:10Here on the screen, I have got opened in my editor the 01-detecting.html
00:16file, and you will notice here the JavaScript is very simple and the HTML is very simple,
00:23so that's the HTML at the bottom of the file.
00:27Here in the JavaScript, all we are doing is on window.onload, which is the way
00:32that I like to load JavaScript, the window.onload event happens after
00:36everything is loaded in the browser, and only then will it call the JavaScript
00:41if I do it this way.
00:43So it calls init, and then init calls this isSupportedBrowserHistory and has
00:50historySupported variable that gets set, and so that variable is declared at
00:54the top of the file, so that's a global variable. And then it calls this
00:57function isSupportedBrowserHistory and that checks for window.history and history.pushState.
01:05If both objects exist then history is considered to be supported, and we test if
01:11history is supported, we get "This browser supports the HTML5 browser history API"
01:16in the statusMessage, and if it's not, we get this other message, "This browser
01:20does not support the HTML5 browser history API."
01:24So let's go ahead and try this in Google Chrome, and I'll click on
01:2901-detecting.html and we see "This browser supports the HTML5 browser history API."
01:37If I try the same thing in Internet Explorer--and I am going to click on that
01:41same link 01-detecting--you see it says, "This browser does not support the
01:46HTML5 browser history API."
01:49So this is how we test for the existence of the API in our browser.
01:55It's a simple test for these two objects, and you can see that's a
02:00relatively simple operation.
02:02Once we know that the browser supports the API, we can begin using it.
Collapse this transcript
Pushing a new URL onto the stack
00:00The first thing we want to do with the HTML5 session history API is to push a
00:04new value onto the history stack.
00:08So what I have here is a working copy of 02-pushstate-start.html.
00:15I made a working copy of that, and I called it 02-pushstate-working.html.
00:20I am actually editing directly on the server.
00:23There is a feature here in Notepad++ that allows me to do that over SSH.
00:28I know on the Mac you can do the same thing with BBEdit, and I believe you can
00:32also do it with BBEdit's free version, which is called TextWrangler. And in any
00:37event, this is the file and I am editing it directly on the server.
00:41Notice that it's very simple and very short.
00:43This is just our starting place.
00:45You see the JavaScript is just a few lines, and the HTML is very, very simple.
00:55So let's go ahead and fire this up in the browser.
00:57This is actually the place where we left off in our last example.
01:01So if I click on pushstate-working--this is the one that I have got opened in my editor--
01:05you see it just says, "This browser supports the HTML5 browser history API."
01:09I will go back to the editor, and I am just going to push a bunch of values
01:16onto the history stack here.
01:19So right after the statusMessage, I am just going to type in a little for loop.
01:22So I am calling the history.pushState method that we tested for in the
01:33isSupportedBrowserHistory.
01:35The first argument is a state object.
01:38It can be any valid JavaScript object.
01:40I am just going to give it this index number here.
01:43This for loop will loop from 0 to 9 and so it will push 10 objects onto the history stack.
01:51Each of these objects will have the state object, which in this case is just that
01:55integer variable, and the next argument is a description.
02:01This is supposed to be what shows up in the browser history.
02:03It's not consistently implemented on all browsers, and so we'll see how this works.
02:08I am just going to say here i is i, value of i there, and the last item is
02:21actually the URL that will go in the history.
02:24In this case, I am just going to put in a slash and the number and .html and that's it.
02:35Go ahead and save that, and we will run it in the browser.
02:39Now, you notice what was here when we pushed this back, we just have a lot of
02:42other stuff in the history. We don't have the things we are pushing now.
02:46When I reload this, you notice that now up here it says 9.html.
02:51It actually pushed each of those onto the stack, and it shows them up there, and
02:55you will notice that I have Browser History Demo.
02:57It did not use the string that we put in there. We'll learn later how to make
03:01that work across browsers.
03:02But when I press this Back button, you see it says 8.html, 7.html, 6.html like
03:08that, and as I go forward, it steps through them forward like that.
03:12We put 10 items on the stack there, 0 through 9, and we've now manipulated
03:18our browser history.
03:19So that's a pretty easy thing to do, isn't it?
03:22Basically, that one line of code, history.pushState, allows you to put whatever you
03:27want to on that history stack.
03:30So pushing an object onto the history stack is really a very simple operation
03:34using the HTML5 session history API.
03:37It's also very easy to retrieve these items once they're there,
03:41so we'll look at how to do that in the next movie.
Collapse this transcript
Handling back and forward buttons with the popstate event
00:00When a user presses the Back or Forward button on the browser, or when
00:04history.go is called in a JavaScript application, the popstate event is fired.
00:09It's relatively simple to implement a handler for this event.
00:13Here we have 03-popstate-working.html, which is a working copy of
00:1803-popstate-start.html. I'm editing this on the server, so we can edit it
00:26and test it in our browser.
00:28You'll notice that it's very simple.
00:30This is really just where we left off from our last lesson.
00:35So here, you see in the init method we are pushing 10 items onto the history
00:40stack, and now we want be able to step through those with the Back button and
00:45get the value from them.
00:47So if you remember, the first argument to this pushState method is a state object.
00:54This state object is what we're going to read off of the stack.
00:58So the state object in this case, it's just the integer from our loop, so it'll
01:01be 0, 1, 2, 3, 4, 5, et cetera, on up to 9.
01:05So we're just going to go ahead in here and we're going to register an
01:11EventHandler for the popstate event.
01:20So we'll say window.onpopstate = popStateHandler, and we'll come up here and
01:28we'll create a function for that.
01:31That gets an event object. And it's pretty simple.
01:41The first thing it does is it tests to see if the event object has a state
01:45attribute, and it simply checks to see if it does not equal null.
01:57Now, in JavaScript it's important to understand that the null object is a special object.
02:03It's not a 0 value, it's not a false value, it's a special object of type null.
02:10And while it will evaluate to false, so does evaluate to false,
02:15so I can't just say if(event.state), if the value of event.state may be an
02:19integer with a value of 0, which in this case we are actually using.
02:23We have to test it for whether or not that attribute is actually set,
02:28and null is how you do that.
02:29So we're testing it against null.
02:31Now, I'm just going to set a statusMessage.
02:38Remember, the statusMessage is a method in bwH5.js JavaScript, and it just sets
02:45a message here in this special statusMessage paragraph on our page.
02:49So I'll just say got event, and state is event.state.
03:01That's all there is to it!
03:03I'm going to go ahead and I'm going to save this, and we'll run it in the browser.
03:06Now, go ahead here and click on popstate-working, and you notice we have
03:12the 9.html up here.
03:14When I press the Back button, we'll see "got event, state is: 8."
03:17If I press the Forward button, "got event, state is 9."
03:21And as I page through this, you see that we have our events.
03:29That's our EventHandler here being called, and that event.state has the integer
03:35in it because we put that in here when we did the pushState. That first
03:38argument is what gets here.
03:39It can be any JavaScript object.
03:41I'm just using an integer here because that's convenient.
03:44In fact, I tend to do it this way anyway in practice because it's really easy to
03:49index into an array or to index into a database with an integer, and it's a
03:53small object, it's easy to carry around-- it doesn't present a lot of performance
03:57challenges for the browser.
03:59It's really, in many, many cases, it's the right way to do this.
04:03There is one more thing I want to show you about this, and you'll notice when I
04:08continue to go back here--and we'll get back to item 1 and item 0--and when I go
04:13back one more, you notice that I get back to my original state, my original URL,
04:18but I don't get a separate state event for that.
04:21That's because we didn't put it on the stack.
04:24That's not done with pushState, that's done with replaceState, and that's what
04:28we're going to learn about in our next lesson.
04:30So reading the value from popstate, again, is an easy proposition.
04:35This is almost enough to build a complete application using the HTML5 session
04:39history API, but we still need to learn about replaceState, and we're going to do
04:43that in our next movie.
Collapse this transcript
Updating the stack with replaceState()
00:00The HTML5 session history API provides two different ways to update the browser history:
00:06pushState and replaceState.
00:08pushState is covered in a previous movie, and it allows you to push an entry
00:12onto the history stack.
00:14replaceState is used to update the current entry on the history stack.
00:18So let's take a look at how this works.
00:20First, we'll start with a working copy of 04-replacestate-start.html, and I've
00:27called this replacestate-working.html.
00:30You'll notice it's substantially the same as where we left off in our last lesson.
00:36But instead of the for loop with 10 different items on the stack, I'm just
00:42pushing two different items onto the stack here:
00:44number 1 and number 2.
00:47I have the popStateHandler to look at them, and let's just take a look at what
00:50this does and what the problem is that we're going to be fixing.
00:54So here's replacestate-working, and I'll run that in the browser here.
00:58You notice it says 2.html. If I press the Back button, it says 1.html, and we've
01:03got an event. The state is 1.
01:05If I press the Back button one more time, you'll notice that there is our
01:08original HTML file, but I did not get an event.
01:12And because I did not get an event, my code doesn't actually know that the user
01:17pressed the Back button again.
01:19There's no new state there.
01:22So if I'm like paging through my images and I want to go back to the first
01:25image, I don't actually have a way of getting there, because there's no state
01:29object in this particular event.
01:32So let's fix this problem and see how that works.
01:36So I'm going to add a line here before my first pushState, and it's important
01:40that this goes before the first pushState because once I start pushing states on
01:44here, I don't have the top of the stack available anymore.
01:47So I'm going to say history.replaceState.
01:54I'm going to give it a number 0, and I'm going to say the number 0.
01:59I'm just going to put a null here in the last argument.
02:04So you remember, the first argument is the state object; the second argument is
02:09a text string that ostensibly is used in the history, and that works in some
02:14browsers and not in other browsers; and the third one is the URL.
02:18Now, because I'm putting a null in the URL, it knows not to replace the URL.
02:24It's there on the stack already.
02:26So this is just going to replace the state object, and the string.
02:30So I'm going to go ahead and save this, and we'll run it in the browser.
02:38I'm just going to click on this, and you notice it goes to 2.html.
02:42In many cases, this isn't going to work.
02:44Yeah, you see it's actually not working. It's not doing what you expect,
02:48and that's because the existing HTML is cached.
02:51So if I just press the Reload button here, and now we'll find that it actually works.
02:57So now we're running the new version, and you notice it says, "got event," and state
03:01is 0, and that's on the original page.
03:04So I'm going to reload it one more time, and you see here it says 2.html.
03:09I go back in my history here and I'll just click this once. I get an event, it
03:13says 1, and we have 1.html.
03:16I click it again, and now I have my original page and I got an event and I have a state.
03:21What's important about this is that now I have a way of actually operating on
03:26that first item in the stack, even though the URL is what it originally was and
03:31Reload works and does what we expect it to do.
03:34Because I'm getting an event, that means I'm able to put something there and
03:38I'm able to control that particular state.
03:41That's very important for our application.
03:43So you'll see we'll use this in our final application.
03:48So now, we have all the pieces we need to fully implement the HTML5
03:52session history API.
03:54In the next movie, we're going to take a look at a complete application.
Collapse this transcript
The complete application
00:00This is a complete application using the HTML5 session history API.
00:05It's a simple photo browser, and this is all implemented in the browser, but of
00:10course in practice it could be a combination of server-side scripting and a
00:15database, and you would probably do it that way for most production applications.
00:19This is designed to be a simple demonstration of this technology and a simple
00:22explanation of how it works.
00:24And indeed, it's a very simple application.
00:27It's only about 90 something lines of JavaScript, and let's take a look at
00:31it real quick here.
00:32Here we have the stuff that goes at the top of the HTML.
00:36There is a little bit of CSS here for formatting of the images and the
00:40little paging buttons.
00:43We have some data at the top of the JavaScript.
00:46These are image URLs and their names.
00:50We test for whether or not the API is supported.
00:53We have a few utility functions here.
00:55Here's our little navigating function, here is for updating the images, setting
01:01the title, our popStateHandler.
01:06We have a little trick here so that we can discern which image is needed from
01:11the file name, and we'll get into a little bit of detail on that in a little bit
01:15here, and there is the init. And finally the HTML is just very, very simple;
01:21this is the whole thing right there.
01:24So let's look at how it runs in the browser.
01:26When it loads up, it loads its initial image, and what it does is it actually
01:31finds the image from the URL.
01:34So you notice that the file name is Pager-Coffee.html. And if we look here at
01:39the code and we'll look at the HTML down at the bottom here, you see our pagerLabel
01:46says placeholder, and the image itself says Placeholder.png.
01:52If I go into the browser here--and I will just navigate up to the Images--
01:57and you'll see Placeholder.png is that image there, and that is not what we're looking at.
02:05When you first load this up you will see the Placeholder loads, and then
02:08once the page is loaded, it loads the image itself, and it puts the word Coffee in here.
02:15And where it finds all of that is right there from the file name, and this is
02:19the code that does that.
02:21It takes the window location, it converts it to a string, and it splits it on
02:25all of the slashes, and then it pops the last element off, and that's the file name itself.
02:31So that's everything up to the last slash, and that's just a little trick for
02:35how I get that part of it.
02:37And now I have just the file name, and I split the file name on the dot and I
02:43take the first element, so that's everything to the left of the dot, and I
02:46split that up on the dash, and I take the last element there, and that gives me the word Coffee.
02:52So it's really just a little trick, but what this allows me to do is to then go
02:57up into my data here and find the Coffee and the Coffee image and load that up.
03:02And the beauty of that is that as I page through this application and as I click
03:09these pager buttons, you will notice it switches to Pager-Keyboard and
03:13Pager-Mouse, and the HTML can be exactly the same.
03:18These files, a Pager-Mouse, Pager- Coffee, Pager-Rock, all of these files
03:24are exactly the same.
03:26In fact, on my server it's just one file and a bunch of symbolic links,
03:31and that saves space. It allows you to update just one file and not have it have to
03:37update the same changes in a number of different files.
03:40The reason I did that is so that I could do this entire application in just HTML.
03:45Normally, I would do something like this with server-side scripting, there would
03:49be just one file, and all of the different URLs would be queries on the one
03:53file, with little question mark at the end and some arguments after it.
03:58But I wanted to do this one all in HTML, just to make it simple for our
04:01purposes of learning.
04:03So the only complexity was, I needed to be able to determine which image it was
04:07from the file name, and so I've got this tricky little bit of JavaScript there
04:10to do that, and that's really all that that's for.
04:14So you will notice as we click on these buttons, it says, "pushing Coffee onto
04:19browser history," "pushing Scissors onto browser history," and when I hold the left
04:23button here you will notice it says Page:Coffee, Page:Keyboard, Page:Mouse, like
04:28that, and that's actually the title from up here. This is,
04:32in some browsers, this is actually what's pushed onto the stack, and if we
04:38come up here to the navigation function, this is what gets fired when we
04:43click on those buttons.
04:44So coming down here to the HTML, you see onClick="nav(-1)," onClick="nav(1)," so
04:50that's for the Back button, passes it a -1, and the Forward button passes it a
04:54plus 1, and so that's our nav function here.
04:58And if history is supported, it calls this makeURI, which just makes the file
05:03name, puts a Pager- in front of it and a .html at the end of it, and then it
05:08pushes it onto the history and does this with window.history.pushState.
05:17And so the first argument is the index. That's just the number that indexes
05:22into this array here.
05:23So it will be a 0 for Rock, a 1 for Paper, a 2 for Scissors.
05:27And then the string here, which is the same as what's in the title bar, and this
05:34file name, which is your Pager- and the image name and the .html, and so that
05:40gets pushed onto the history.
05:42Now, what I discovered, different browsers will do this in different ways, as far
05:47as what strings it puts in the history list there.
05:51Chrome will put whatever's in the title, and it just grabs the title and it
05:55ignores that second argument to pushState.
05:58Firefox will only put the title in if the title has changed; otherwise, it just
06:04puts the URL in there.
06:06Safari does exactly what you would expect it to do.
06:10It uses that second argument string from the pushState. And if you look in the
06:14spec, it says that the browsers are free to do this however it is that they want to do it.
06:19I would prefer that there is some standard to it, but there isn't, and so by
06:24setting the title each time, I was able to get this to work right.
06:28And so I have a little function here called setTitle, and every time we navigate
06:34we call setTitle, and setTitle is right here.
06:37All it does is it sets document.title to the title that we want.
06:43So the rest of this is all very straightforward.
06:46We have window.onload. We call init. init checks to see if the history API is
06:51supported, and if not, it puts up a nice little alert, and we saw that in
06:57Internet Explorer in our Overview movie.
07:00It registers the popStateHandler, and here is the popStateHandler right here.
07:05It's very, very simple.
07:07It checks to see if the event.state is null.
07:10It simply copies it to that global index variable.
07:14It puts up a statusMessage saying that it got the name of the image from the
07:18history, and it updates the image with the index, and it sets the title.
07:23It's as simple as that.
07:25And all that's getting stored on the stack is that integer, that index integer.
07:32index is a global variable here.
07:34It gets set whenever we want to change things and then it gets used for updating
07:37the images in the title and everything else.
07:40It's all very, very straightforward.
07:43So the first thing we do is we get the current index from path and again,
07:47that's this function here that gets the path array and finds the file name and
07:50decides which element out of the array it is, and it sets the index with that.
07:56It puts up a statusMessage saying what the initial image is.
07:59It checks if the history is supported, and if not, it does replaceState, and
08:05this allows us to use the Back button to get all the way back to that first one.
08:10So if I go all the way back here and just click on Pager-Coffee--so that's our
08:18first one--and if I'll just page forward a couple of them here and I'll page
08:22back a couple of them, and you notice that it works, even for the first one.
08:28And that's because we did this replaceState as soon as we load it up.
08:32We update the image, we set the title, and we set the little app version there,
08:37that's this string right here in our little h1 tag. And you notice that that is
08:42right here, appVersion. So that's it.
08:46It's very, very simple.
08:48The advantages of the HTML5 session history API are all present here.
08:52Performance is maximized by keeping network traffic to minimum, but each photo
08:56has its own fully functional URL.
09:00Click forward here to say the Mouse, and I will just grab this URL and put it in
09:05my Bookmark bar here.
09:07And I can just bring up Google.com, and when I click on Page:Mouse, there is it.
09:14It's a completely functional bookmark, and yet, as I press these buttons and as
09:20I press the Back button up here, it's not going out to the network and loading that up again.
09:26So this is a common application for the HTML5 session history API to use it for
09:30a photo browser, and here we have it fully implemented in less than 100 lines
09:35of JavaScript.
Collapse this transcript
Conclusion
Goodbye
00:00In this course my goal was to give you a good understanding of the HTML5 session
00:05history application programming interface.
00:07I've covered how the API works, how to push items onto the history stack,
00:12and how to register an event handler to receive notification of history
00:15events, so you may update items on your page when a user presses the Back and Forward buttons.
00:20As with most of the HTML5 application technologies, the specifications for HTML5
00:26session history remain in flux.
00:29I suggest that you keep up with these changes on the Web Hypertext Application
00:33Technology Working Group web site in order to make sure that your code works
00:37with current and future browsers.
00:39And be sure to check out the other HTML5 titles here on the lynda.com
00:43Online Training Library.
00:46As always, feel free to use my working examples as a starting point for your own
00:50applications, and I look forward to seeing what you do with the HTML5 session
00:54history API in your own projects.
Collapse this transcript


Suggested courses to watch next:

HTML5: Structure, Syntax, and Semantics (4h 34m)
James Williamson

HTML5: Geolocation in Depth (34m 19s)
Bill Weinman



Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 98,468 instructional videos.

start free trial learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 1,893 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.


site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked