Get the previous URL with javascript

7

How do I get the previous URL of the current page in javascript?

    
asked by anonymous 12.12.2014 / 17:19

1 answer

8
document.referrer

According to MDN in my own translation;

  

Returns the URI of the page that made the link. Returns an empty string if the user navigated to the page directly (not through a link, for example, through a bookmark ). Since this property returns a string it does not give DOM access to the referenced page

This property is in the DOM specification , so it is cross browser , however you can test whether the browser implements it in this way;

if( typeof document['referrer'] !== 'undefined' ) { 
    //Seu browser suporta document.referrer
}

Note: Some versions of IE do not always define this property; view this thread

Note 2: You may be interested in worrying about anti-XSS security when using document.referrer , is an issue already raised by others

    
12.12.2014 / 17:29