Get data using current URL with JavaScript


1 min read

You can get the URL object using window.location.href like this:

let url = new URL(window.location.href)

Then, parse that URL with split to get the data you need:

let id = url.pathname.split('/')[2]

In this case, the URL I got was The pathname is equal to /request/1/update/. The 1 is the ID of the data object I need.

Once you've got the ID, you can use it in an axios call to get the data you need.