46

How to get previous page URL using jQuery?

I am using the following code to get the current page location

$(document).ready(function() {
var pathname = window.location.pathname;
});
Syscall
  • 19,327
  • 10
  • 37
  • 52
Geeth
  • 5,282
  • 21
  • 82
  • 133

9 Answers9

107

Easy as pie.

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

It is not always available though.

starball
  • 20,030
  • 7
  • 43
  • 238
Marcos Placona
  • 21,468
  • 11
  • 68
  • 93
  • Note to Geetha: document.referrer is plain old JavaScript, so that might the reason that you can't find it browser the jQuery docs. – Jan Aagaard Mar 10 '10 at 09:18
  • It doesn't seem to work for me. How can I write the output in a input value? – curly_brackets Dec 23 '10 at 12:46
  • just bellow the var you could make it write this to a text field for example... document.getElementByID("myField").value=referrer; You could write it to a div as well. Many different ways – Marcos Placona Sep 29 '11 at 15:57
  • To put it in an input value using jQuery: $('#input-id').val(document.referrer); – Marshall Æon Jan 16 '12 at 00:49
  • 1
    I needed to implement my GoBack() js function then i tried this function GoBack() { history.go(-1) } but this has an issue when you deal with confirmation messages but document.location.href = document.referrer; worked as a pretty .... Many thanks Marcos : ))) – N0rA Feb 12 '14 at 15:25
  • This is ok and working fine but can we get only domain from referrer? @MarcosPlacona – Gobind Gupta Nov 02 '18 at 07:53
  • Hey @GobindGupta have a look at this answer https://stackoverflow.com/questions/10317017/javascript-get-domain-only-from-document-referrer – Marcos Placona Nov 23 '18 at 11:48
  • @MarcosPlacona, Thanks for efforts, i have also got the answer. – Gobind Gupta Nov 23 '18 at 18:15
9

Do you mean something like history.go(-1);? It will go back to the previous page.

window.history on MDN

kapa
  • 77,694
  • 21
  • 158
  • 175
Grumpy
  • 2,140
  • 1
  • 25
  • 38
7

If you are using PHP, you can check previous url using php script rather than javascript. Here is the code:

echo $_SERVER['HTTP_REFERER'];

Hope it helps even out of relevance :)

Roel B
  • 105
  • 3
5
var from = document.referrer;
console.log(from);

document.referrer won't be always available.

Amarghosh
  • 58,710
  • 11
  • 92
  • 121
3

We have document.referrer for this, but it is not on which we can relay. This could be saved, could be not

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

The better approach is to add a cookie to store the previous-url in the browser.

Aman Jain
  • 655
  • 5
  • 17
2

simple & sweet

window.location = document.referrer;
Moti Korets
  • 3,738
  • 2
  • 26
  • 35
0

Use can use one of below this

history.back();     // equivalent to clicking back button
history.go(-1);     // equivalent to history.back();

I am using as below for back button

<a class="btn btn-info float-right" onclick="history.back();" >Back</a>
Jignesh Patel
  • 166
  • 11
-1
    $(document).ready(function() {
               var referrer =  document.referrer;

               if(referrer.equals("Setting.jsp")){                     
                   function goBack() {  
                        window.history.go();                            
                    }
               }  
                   if(referrer.equals("http://localhost:8080/Ads/Terms.jsp")){                     
                        window.history.forward();
                        function noBack() {
                        window.history.forward(); 
                    }
                   }
    }); 

using this you can avoid load previous page load

Sameera
  • 47
  • 1
  • 1
  • 6
-3

document.referrer is not working always.

You can use:

window.location.origin
mspapant
  • 1,860
  • 1
  • 22
  • 31
  • `window.location.origin` provides the root of the website NOT the referrer. e.g. `http://stackoverflow.com/questions/1420881/javascript-jquery-method-to-find-base-url-from-a-string` (which incidentally is my reference) becomes `http://stackoverflow.com` – dav_i Jan 25 '13 at 16:03