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 http://127.0.0.1:8000/request/1/update/. 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.