4

I am working on an application in which the user deals with tickets. On each ticket the user can click a "Go Back" button, and is sent to the previous page.

The function is using window.history.back() and verifies before that the history is not empty! But, there are two issues to this method :

1. when the user wants to access the ticket via the URL, he firsts has to sign in, then if he clicks on the "Go back" button, he is redirected to the login page.

2. when the user comes from an other site, the "Go back" button makes him return to the previous site.

I read a lot about the history things and so, but I did not find any solution to get the previous URL as a string without going to this. I also already tried the referrer things without success.

Thank you for your answer!

Diane Duquesne
  • 516
  • 2
  • 11
  • 2. Logically there's no history back that resolves to your website. 1. Store the credentials into session. – Roko C. Buljan Aug 26 '15 at 21:42
  • First result on Goog: http://stackoverflow.com/questions/3528324/how-do-you-get-the-previous-url-in-javascript – Roko C. Buljan Aug 26 '15 at 21:45
  • Using strictly client side solution, you're stock to JS and issues like you mentioned (you can use document.referer to check). Just wanted to also share this resource for you to explore. You can also create a back button by rendering a link, whose resource/url is determined server side using HTTP_REFERER variable. This variable is not always available (the links below describe when you can rely on it). for reference: - http://www.sightspecific.com/~mosh/www_faq/back.html - https://en.wikipedia.org/wiki/HTTP_referer – zagros Aug 26 '15 at 21:51

2 Answers2

-2

In javascript you may use

document.referrer
Mexflubber
  • 85
  • 1
  • 7
-2

You can use this function:

document.referrer

The value is an empty string if the user navigated to the page directly (not through a link, but, for example, via a bookmark). Since this property returns only a string, it does not give you DOM access to the referring page.

Read about it here

Starfish
  • 3,344
  • 1
  • 19
  • 47