How do I get the previous URL of the current page in javascript?
How do I get the previous URL of the current page in javascript?
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