hrishikeshp19
hrishikeshp19

Reputation: 9026

YUI browser history manager back button

In am using YUI browser history manager for keeping track of Ajax navigations. I am registering history object like:

YAHOO.util.History.register("state",init, onStateChange);

Here onStateChange is getting called when I do

YAHOO.util.History.navigate("state",urlhash);

and when I press back button.

Is there any way to know if onStateChange is called on back button or by calling navigate?

Upvotes: 1

Views: 423

Answers (1)

DannyMeister
DannyMeister

Reputation: 1303

If this event can be called by an external system sometimes (in this case the browser back button) and by your own code sometimes, you can differentiate the caller by making it a requirement to do something special when your own code calls the method. Wrapping the call to navigate() in your own helper method can help make sure you stay consitent with this.

In the YUI docs I see that register() can take an optional 4th parameter (among others) which is an arbitrary object that will get passed into your onStateChange handler function. One of the properties of this object could be a flag indicating that it was called from your code versus initiated by the browser, and in your navigate() wrapper always set that flag. Remember to set it back in your handler.

Note: I am not as familiar with YUI as ExtJS, maybe some guru that knows the API better can help, but this is a general strategy that can work. This answer makes an inelegant assumption that a call to navigate() will make that handler fire reliably before any other navigation has a chance to occur, which is probably a safe bet in the single-threaded javascript world.

Upvotes: 1

Related Questions