-1

How to navigate to previous browser url on button click?

Already tried this suggestions:

https://developer.mozilla.org/en-US/docs/Web/API/History_API

Onclick javascript to make browser go back to previous page?

Problem is that with window.history.go(-1) it navigates to homepage '/' url not to the previous page url.

Code:

<a class="link-terminal" href="" onclick="goToPreviousPage(); return false;"></a>

<script>
   function goToPreviousPage() {
      window.history.go(-1);
   }
</script>
Paulius Rimgaila
  • 316
  • 2
  • 3
  • 13
  • 3
    Possible duplicate of [Onclick javascript to make browser go back to previous page?](https://stackoverflow.com/questions/8067510/onclick-javascript-to-make-browser-go-back-to-previous-page) – Luis felipe De jesus Munoz Jun 27 '18 at 14:46
  • Already tried all suggestions in this link, doesn't work in my case – Paulius Rimgaila Jun 27 '18 at 14:47
  • Be certain, .go and .back work as intended. Clearly the history isn't what you expect, start there and inspect page loads etc. – RamblinRose Jun 27 '18 at 14:56
  • Missed this - have goToPreviousPage return false. Also, see this article https://stackoverflow.com/questions/5422770/returning-false-from-click-handler-doesnt-work-in-firefox – RamblinRose Jun 27 '18 at 15:03
  • There's likely something else going on in your application if those functions don't work. Check for errors in the console, let us know if you're using some kind of framework, etc. – Heretic Monkey Jun 27 '18 at 15:14
  • After adding return false; it navigates to homepage '/' route, but not to previous visited url. I need to use plain javascript in this case. – Paulius Rimgaila Jun 27 '18 at 15:23
  • Result after click: Navigating to homepage, page title shows last visited page title as it should, but page url not changed. – Paulius Rimgaila Jun 28 '18 at 03:40

1 Answers1

0

<head>
</head>
<body>
<a class="link-terminal" href="" onclick="goToPreviousPage(); return false;">go back</a>

<script>
   function goToPreviousPage() {
      window.history.go(-1);
   }
</script>
</body>

Be sure you add the return false; part in your onclick.

This should fix your problem.

Also, in your code //window.history.go(-1)) has one too many ")" at the end.

Grant Miller
  • 27,532
  • 16
  • 147
  • 165
  • Now it navigates to homepage '/' route, but not to previous visited url. – Paulius Rimgaila Jun 27 '18 at 15:24
  • Result after click: Navigating to homepage, page title shows last visited page title as it should, but page url not changed. – Paulius Rimgaila Jun 28 '18 at 03:40
  • i googled around a bit and i found this post that looks very similar. https://stackoverflow.com/a/20421071/5497395 . are you running firefox ? – JanWillem Huising Jun 28 '18 at 19:06
  • Cant use that approach, console error: `Uncaught DOMException: Failed to execute 'pushState' on 'History': A history state object with URL 'javascript:window.history.back();' cannot be created in a document with origin` – Paulius Rimgaila Jun 29 '18 at 03:32
  • Adding javascript, e.preventDefault(), navigates to specified href="/" url not to previous page and url not updating, just page title shows correct previous page title – Paulius Rimgaila Jun 29 '18 at 03:50