The History API Module #35 Flashcards

1
Q

What are the 3 methods used to engage with the History API? How is go( ) different from the rest?

A
  1. back( )–Goes to the previous entry in session history
  2. forward( )–Goes to the next entry in session history
  3. go( )–Allows navigation backward or forward through multiple levels
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you determine how many levels there are in the session history?

A

history.length

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would you put together a function that returns the user to a previous history state?

A

First hook into the dom element such as a button with something like querySelector. Then attach an event listener to the button, that listens for a “click” event. Finally fire the history( ) method on the event. Example:

document.querySelector(‘.back’).addEventListener(‘click’, ( ) => {
history.back( )
} )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How would you go back or forward through multiple history states?

A

Use the go method. For example:

history. go(-1) //equivalent to history.back( )
history. go(-2) //equivalent to calling history.back( ) twice
history. go(1) //equivalent to history.forward( )
history. go(3) //equivalent to calling history.forward( ) 3 times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How is an extra entry added to the history state?

A

Using pushState( )

const state = { color: 'red' }
history.pushState(state, ' ', '/anotherPage')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the 3 params you pass when using the push state method

A
  1. an object which can contain anything OR be empty
  2. Second param is currently unusable by browsers so make it an empty string (“”)
  3. The third is a URL associated with the new state. BUT the url must belong to the same domain of the current URL.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What technique is used to manipulate the current history state?

A

replaceState( )

This accepts the same params see below:

history.replaceState(state, ‘ ‘, ‘/post/second’)

AGAIN:
1. The first is an object which can contain anything (there is a size limit however, and the object needs to be serializable).

  1. The second parameter is currently unused by major browsers, so you generally pass an empty string.
  2. The third parameter is a URL associated to the new state. Note that the URL needs to belong to the same origin domain of the current URL.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What on Earth is ‘popstate’ used for?

A

This event is called on window every time the active history state changes with the current state as the callback parameter.

EXAMPLE:

window.onpopstate = event => {
console.log(event.state)
}

.state refers to the current state!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a function using popstate as an event listener.

Hint: Start with window.addEventListener

A

window.addEventListener(‘popstate’, event => {
console.log(event.state)
} )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly