Control the URL bar with JavaScript

232 Views Asked by At

Is there some function in javascript or React.js to tell if a user tries to access an existing page through the URL bar?

2

There are 2 best solutions below

0
Kevin Gilbert On

In plain javascript: window.history How do I modify the URL without reloading the page?

In React, use should use React Router along with useHistory hook : https://reactrouter.com/web/guides/quick-start

0
Jad Al-Hamwi On

You can use useHistory hook:

import {useHistory} from “react-router-dom”

Then :

const {location} = useHistory()
console.log(location.pathname)