Grigor
Grigor

Reputation: 4049

Javascript history.pushState

I found example of history.pushState() through stackoverflow but I don't understand what each thing does. this is what I have

var stateObj = {
    foo: "bar"
};
history.pushStates(stateObj, "page 2", "page2.html");

so can anyone explain what stateObj holds and what "page 2" is in this script? Why does the object have foo: "bar"?

Thanks in advance.

Upvotes: 2

Views: 4954

Answers (2)

Dan O
Dan O

Reputation: 6090

this link might be of use:

https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history#The_pushState%28%29.C2.A0method

the first parameter, stateObj in the example, is an arbitrary context object containing whatever you'd like. It's accessed when the user visits a different page and then navigates back to your page using their browser's Back button. see the popState event for more information there.

the second paramater is currently unused; it's recommended to pass the empty string here.

the last parameter is the URL associated with the context object. It does not change the location of the current page.

Upvotes: 6

Related Questions