Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
103 views
in Technique[技术] by (71.8m points)

How does the Back button in a web browser work?

I searched the Web about this question but I found nothing:

What is the logic of the back button? What is happening when we hit the back button on a Web browser?

I really would like to understand more about that.

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your web browser keeps a stack (or list, if you will) of the web pages that you have visited in that window. Let's say your home page is google.com and from there you visit a few other websites: youtube.com, yahoo.com, and cnn.com. Upon visiting the last one, the list looks like this:

google.com -> youtube.com -> yahoo.com -> cnn.com
                                            ^
                                            |
                                       current page

When you press the Back button, the browser takes you back to the previous page in the list, like this:

google.com -> youtube.com -> yahoo.com -> cnn.com
                                ^
                                |
                           current page

At this point you can press Back again to take you to youtube.com, or you can press Forward to put you at cnn.com again. Let's say you press Back a second time:

google.com -> youtube.com -> yahoo.com -> cnn.com
                   ^
                   |
              current page

If you now go to, say, abc.com, the list changes to look like this:

google.com -> youtube.com -> abc.com
                               ^
                               |
                          current page

Note that both yahoo.com and cnn.com are gone from the list. This is because you took a new route. The browser only maintains a list the pages you visited to get to where you are now, not a history of every page you've ever been to. The browser also doesn't know anything about the structure of the site you're visiting, which can lead to some surprising behavior.

You're on a shopping site (ne.com, as a short example) that has categories and subcategories of products to browse through. The site designer has thoughtfully provided breadcrumbs near the top of the window to allow you to navigate through the categories. You start at the top page of the site, click on Hardware, then Memory. The list now looks like this:

google.com -> ne.com -> ne.com/hw -> ne.com/hw/mem
                                           ^
                                           |
                                      current page

You want to go back to the Hardware category, so you use the breadcrumbs to go up to the parent category instead of using the Back button. Now the browser list looks like this:

google.com -> ne.com -> ne.com/hw -> ne.com/hw/mem -> ne.com/hw
                                                          ^
                                                          |
                                                     current page

According to the site structure, you went backward (up a level), but to the browser you went forward because you clicked on a link. Any time you click on a link or type in a URL in the address bar, you are going forward as far as the browser is concerned, whether or not that link takes you to a page that you've already been to.

Finally, you want to return to the main site page (ne.com). You could use the breadcrumbs, but this time you click the Back button -- it seems obvious that it should take you up one level, right? But where does it take you?

It's initially confusing to many users (myself included, when I happen to do exactly this) that it takes you "down" a level, back to the Memory category. Looking at the list of pages, it's easy to see why:

google.com -> ne.com -> ne.com/hw -> ne.com/hw/mem -> ne.com/hw
                                            ^
                                            |
                                       current page

To go back to the main page using only the Back button would require two more presses, taking you "back" to the Hardware category and finally to the main page. It seems so obvious to us programmers what's going on, but it surprises the heck out of regular users all the time because they don't realize that the browser doesn't know anything about the hierarchical structure of whatever website they happen to be on.

Would it be great if browsers would let site designers program the Back button to do the obvious thing (take you up a level) rather than whatever it does now?

Edit: A commenter asked whether the browser reloads the page or simply displays it out of its local cache.

The answer is it depends. Site designers can specify whether the browser should cache the page or not. For pages that are set as non-cached, the browser reloads the page from the server when you press Back, as though it was the first time you are visiting it. For cached pages, the browser displays it out of the cache, which is much faster.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...