Using the BACK button to revert to the previous state of the page

China☆狼群 提交于 2019-12-29 08:41:29

问题


I am trying a new functionality for my web site. I want to do simple navigation by hiding/showing <div> elements.

For example, when a user clicks a "details" button on some product, I want to hide the main <div> and show the <div> containing the details for the product.

The problem is that to go back to the previous "page", I have to undo all the display/visibility style changes, which is ok if the user clicks the "close" button in the newly opened <div>. But most users will hit the BACK button.

Is there a way to make the BACK button go back to the previous "state" of the page i.e., undo the visibility/display changes?

Thanks.


回答1:


to answer the question of your title (that's what I was looking for)

Using the BACK button to revert to the previous state of the page

and from the link from @reach4thelasers's answer, you have to set up a timer and check again and again the current anchor:

//On load page, init the timer which check if the there are anchor changes each 300 ms  
$().ready(function(){  
   setInterval("checkAnchor()", 300);  
});

because there's no Javascript callback triggered when the BACK button is pressed and only the anchor is changed ...

--

by the way, the pattern you're talking about is now known as Single Page Interface !




回答2:


Yes. What you're looking for is called AJAX browser history.

There are a few open implementations out there, like RSH as well as plugins/modules for frameworks like jQuery and YUI.




回答3:


You need to add an anchor to the URL whenever a change is made

www.site.com/page.html#anchor1

This will allow the browser to maintain the pages in its history. I implemented it in my current site after following this tutorial, which works great and gives you a good understanding of what you need to do:

http://yensdesign.com/2008/11/creating-ajax-websites-based-on-anchor-navigation/

Your example in the comments won't work, because it works like this:

  1. Page Loaded

  2. Page Changed, Add Anchor to URL (back button takes you back to back to 1)

  3. Page Changed, Anchor Changed (back button button takes you back to 2)

  4. Page Changed, Anchor Changed (back button button takes you back to 3)

    .... and so on and so on..




回答4:


If there is, it sounds like a pretty evil thing to do from a UX perspective. Why don't you design a "back" button into your application, and use design to make it obvious to the user that they should use your application's back button instead of the browser.

By "use design," I mean make your application look like a self-sufficient user interface inside of the browser, so the user's eye stays within your page, and not up on the browser chrome, when they are looking for controls to interact with your app.




回答5:


You can do this with anchors, which is how it's done in a lot of flash applications, or other apps that don't go from page to page. Facebook uses this technique pretty liberally. Each time the user clicks on a link that should go in their history, change the anchor on the page.

So say my home page link is:

http://www.mysite.com/#homepage

For the link that works your javascript magic, do this:

<a href="#otherpage" onclick="javasriptMagic()">My Other Page</a>

This will send the user to http://www.mysite.com/#otherpage where clicking the back button will go back to http://www.mysite.com/#homepage. Then you just have to read the anchors with

window.location.hash

to figure out which page you're supposed to be on.




回答6:


Take a look to this tutorial based on ItsNat a Java web framework focused on Single Page Interface web sites



来源:https://stackoverflow.com/questions/1732378/using-the-back-button-to-revert-to-the-previous-state-of-the-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!