0

I am new to JavaScript and I have the following doubt.

Into a page I find this link to go back to the previous page:

<a href="javascript:history.back()"><span class="glyphicon glyphicon-menu-left"></span> Indietro</a>

My doubt is:

it works fine (come back to the previous page) but it seems to me that the javascript:history.back() does not generate an HTTP request because:

  1. If I open the Network tab inside FireBug I can't see no generated request here.

  2. I am working on a Spring MVC application and when I click here there is no controller methods that handle a request.

Is it true or am I missing something? How does it work? What am I missing?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • You should avoid `history.back()` for most uses, and just use a standard link. You cannot be sure that "back" actually makes any sense. If they opened your page from a bookmark, "back" could take them completely away from your site. – user229044 Apr 11 '16 at 16:21

2 Answers2

2

history.back() will generally be able to load the page from recent cache, so will not cause a network fetch. In my experience the server is not checked for an updated page expiry header, expiry is checked based on what is in the cache.

Common exceptions to this are if the browser can tell that the page or some of its content has Expired or is non cachable, or if the page was POSTed, or fetched with a query string in the URI.

As this is a browser built-in, it is possible that this behaviour varies from browser to browser, and may change in future.

Steve Davies
  • 106
  • 6
1

The history.back() method is just utilizing the browsers native back button capability. Therefore it is not generating an HTML request. You could however track this by creating a back button on your page, and listen for an onClick() event. Once the event is called call and ajax request to some backend endpoint to log the event, then on success called history.back()

Nick Delaney
  • 601
  • 5
  • 15