JavaScript: Retrieving the Previous URL
Problem:
Navigating web pages often requires access to the previous URL without using anchors or cookies. Is there a JavaScript solution to this?
Answer:
Yes, the document.referrer property can provide the URL of the page visited before the current one. This property is typically populated when users click a link to navigate to the current page, but it may not be accessible in all cases.
Explanation:
For security and privacy reasons, window.history cannot be used to access URLs in the session. Access to historical URLs would allow websites to track user activity across different websites.
Usage:
console.log(`Previous URL: ${document.referrer}`);
Considerations:
While document.referrer can provide the previous URL, it may not be available in some scenarios, such as when users:
Alternatives:
For reliable state management within your own site, consider using:
The above is the detailed content of How Can I Retrieve the Previous URL in JavaScript Without Using Cookies or Anchors?. For more information, please follow other related articles on the PHP Chinese website!