Conditional JavaScript navigation

If you have paginated content - and Django makes it really easy to paginate your overview pages - then you may want to have "backwards" links on the detail pages which link back onto the page, on which the user clicked on the object, for example the user browses from page 1 to page 2 to page 3 and then clicks on a headline to read an entry. After reading the page the user clicks the backwards link and expects to get back to page 3.

I'm sure there are various ways how to code this in your backend, but I solved it with this one-liner in the frontend:

<a onclick="javascript:if(history.length>1){
    history.back();return false;}"
    href="/list/">back</a>

This solution degrades in two ways: Without JavaScript the user always returns to the first page of the paginated content (here: /list/) and If the user has clicked a link somewhere else or in an email there is no history for our page and the user also returns to the first page of the paginated content.

I've tested it in Firefox, Safari, Opera and Internet Explorer.