Javascript history.pushState

Go To StackoverFlow.com

2

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.

2012-04-04 20:08
by Grigor


5

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.

2012-04-04 20:14
by Dan O
and FYI, this is not specific to jQuery - Dan O 2012-04-04 20:16
so what can be instead of foo: "bar" and what does it do there - Grigor 2012-04-04 20:30
any data that you might want to store to access later during the popstate event - dontGoPlastic 2012-04-04 20:36
that depends on your application! Put some important variables in there so that if the user navigates away from your page and then goes back to it, they can pick up right where they left off - Dan O 2012-04-04 20:36


0

https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history#The_pushState().C2.A0method

2012-04-04 20:12
by dontGoPlastic
Please try to expand on your answer - vcsjones 2012-04-04 20:15
@vcsjones How so? OP basically asked for documentation, which I linked to - dontGoPlastic 2012-04-04 20:20
Ads