linking to the previous page javascript document.referrer

5.5k views Asked by At

I am trying to implement a link that will take me back to the page it last came from. So on site A I submit a form and it goes to site B where when a link is clicked it should go to site A.

here are the code for the two pages. site A:

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>
<a href="http://www.example.com/testbackpage2.html">link to page 2</a>
<p>My first paragraph.</p>

</body>
</html>

site B:

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>
<script type="text/javascript">
var ex=document.referrer;
document.write(ex);
</script>
<a href="ex">go back to page one</a>
<p>My first paragraph.</p>

</body>
</html>

</body>
</html>
4

There are 4 answers

2
Jamiec On

To go back one page simply use window.history.go(-1) or window.history.back()

<a href="#" onclick="window.history.go(-1)">go back to page one</a>

More info on this API can be found here: https://developer.mozilla.org/en-US/docs/Web/API/Window/history

3
Sunil Dora On

You can try like this

(How to get previous page url using jquery)

$(document).ready(function() {
  var referrer =  document.referrer;
});

Or

you can also try,

<a href="javascript: history.go(-1)">Back</a>

From this link

Hope it helps.

0
Praveen Kumar Sharma On

use simple to go back in Jswindow.location = document.referrer;

2
very tall On
top.location = document.referrer;