Rizal Ibnu
Rizal Ibnu

Reputation: 443

How to get starting URL when user visit to the website in React JS or React Router?

How to get starting URL when user visit to the website in React JS or React Router ?

I want to know user is visited from what page in my single web application built with React JS v16.

Upvotes: 1

Views: 2025

Answers (2)

Rizal Ibnu
Rizal Ibnu

Reputation: 443

Thank you for all answer, but I have found the best answer by myself.

To do this, when user first come to website, we need to save the first current page address (URL) to HTML5 local storage with key "startingURL" in react component outside of route component, for example in App.js file.

localStorage.setItem("startingURL", window.location.href);

To get starting URL, simply get local storage item with key "startingURL"

localStorage.getItem("startingURL");

Upvotes: 1

kugtas
kugtas

Reputation: 113

this should be possible with plain javascript

document.referrer

For more see this question: How to get the previous URL in JavaScript?

Or the documentation: https://developer.mozilla.org/en-US/docs/Web/API/Document/referrer

Upvotes: 0

Related Questions