You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
155
155
156
156
```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
+
};
160
167
```
161
168
162
169
## 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
232
239
```javascript
233
240
// handle forward/back buttons
234
241
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
+
}
238
248
239
249
};
240
250
```
@@ -244,10 +254,6 @@ Now let's check for that truthy value and use a turnary statment to switch our c
0 commit comments