Skip to content

Commit fc30f7c

Browse files
committedMar 7, 2013
Moved popstate guard to popstate event
1 parent 043cc5b commit fc30f7c

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed
 

‎README.md

+17-11
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,19 @@ window.onpopstate = function(evt) {
151151
};
152152
```
153153
154-
This poses a slight problem though. Chrome fires the popstate event on page load. So this code will fire on your initial page load and try to pass an empty state object to our display function. This will cause and error because our function expects there to be data in that state object. Let's guard against this by adding a line to the beginning of our displayContent function.
154+
This poses a slight problem though. Chrome fires the popstate event on page load. So this code will fire on your initial page load and try to pass an empty state object to our display function. This will cause and error because our function expects there to be data in that state object. Let's guard against this by adding a guard around our function call.
155155
156156
```javascript
157-
// chrome inits with popstate
158-
// so bail out if state is null
159-
if (state === null) { return; }
157+
// handle forward/back buttons
158+
window.onpopstate = function(evt) {
159+
160+
// chrome inits with popstate so check for state
161+
if (evt.state) {
162+
// get the state and change the page content
163+
displayContent(evt.state);
164+
}
165+
166+
};
160167
```
161168
162169
## Step 8: Handle the first page
@@ -232,9 +239,12 @@ Our content now animates in and out as we navigate through our site. It makes se
232239
```javascript
233240
// handle forward/back buttons
234241
window.onpopstate = function(evt) {
235-
236-
// get the state and change the page content
237-
displayContent(evt.state, true);
242+
243+
// chrome inits with popstate so check for state
244+
if (evt.state) {
245+
// get the state and change the page content
246+
displayContent(evt.state, true);
247+
}
238248

239249
};
240250
```
@@ -244,10 +254,6 @@ Now let's check for that truthy value and use a turnary statment to switch our c
244254
```javascript
245255
function displayContent(state, reverse) {
246256

247-
// chrome inits with popstate
248-
// so bail out if state is null
249-
if (state === null) { return; }
250-
251257
// change the page title
252258
document.title = state.title;
253259

‎script.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ if (window.history && 'pushState' in history) {
99

1010
function displayContent(state, reverse) {
1111

12-
// chrome inits with popstate
13-
// so bail out if state is null
14-
if (state === null) { return; }
15-
1612
// change the page title
1713
document.title = state.title;
1814

@@ -97,9 +93,12 @@ if (window.history && 'pushState' in history) {
9793
// handle forward/back buttons
9894
window.onpopstate = function(evt) {
9995

100-
// get the state and change the page content
101-
displayContent(evt.state, true);
102-
96+
// guard against popstate event on chrome init
97+
if (evt.state) {
98+
// get the state and change the page content
99+
displayContent(evt.state, true);
100+
}
101+
103102
};
104103

105104
// create state on page init and replace the current history with it

0 commit comments

Comments
 (0)
Please sign in to comment.