How to distinguish browser back / forward button?
Is it possible? even better, is it possible wi开发者_JAVA技巧th jQuery?
I need to know whether use click back button or forward button so I can use page transition effect correctly, eg. slide from left - right if they hit forward and vice versa.
You can use the really simple history library
But if you want to handle it yourself, you can use the hash key (window.location.hash
) by setting for instance the slide number there.
When the user will click back or next the hash key will change, you detect it and make the change.
Unfortunately you have to check the change yourself, by setting an interval(i.e: 300ms) and check with the previous value. HTML5 has an onhashchange event.
@David
Since browser back buttons don't track ajax requests, developers have no choice but to suppliment this lack of functionality. Using jquery we have to manually keep track of ajax requests and manually re-request the partial pages in order to give users what they WANT and EXPECT.
A user can go to a previous page without clicking Back button in the browser. He can right click and from the context menu he can choose the option Back or Forward. He can hit the keyboard also to navigate to another page. So it won't be a good idea to catch the Back button click.
You can use history object.It has the array of visited pages. The most recent entry is the last forward or back button page.
One of our guiding design principles is this:
NEVER, NEVER, NEVER BREAK THE BACK BUTTON.
This means not interfering with its operation, which means leaving it alone.
If you MUST know which button the user has pressed, you're pretty much out of luck, and this is by design. The guys who write the browsers don't want you to do this. I don't want you to interfere with back button functionality. Users don't want you to. So, DON'T.
/rant.
精彩评论