Arasto
Arasto

Reputation: 491

Get id from URL parameters

My project is using React 16.9^ with Hooks and I'm trying to get the parameter from the URL.

http://localhost:3000/kontakt/confirm-account?token=d5b72ed2-ebb3-4da9-8619-1223234950a5

I need to detach the last part:

confirm-account?token=d5b72ed2-ebb3-4da9-8619-1223234950a5


Whats the best practice to do this? Any suggestions on how this could be done would be much appreciated.

Upvotes: 2

Views: 6364

Answers (6)

Mara Black
Mara Black

Reputation: 1751

Another example: w3schools snippet

var allUrl =  window.location.href
var result = window.location.href.split('/').reverse()[0]

document.getElementById("fullUrlId").innerHTML = "full url: " + allUrl ;
document.getElementById("resultId").innerHTML = "url after split: " + result;
<div id= "fullUrlId"> url </div>
<div id= "resultId"> result </div>

Upvotes: 1

Tony
Tony

Reputation: 910

if you are trying to do token based authentication system you can send token on every request header x-access-token and verify that on server side (middleware)

enter image description here

Upvotes: 0

Fabian Bron
Fabian Bron

Reputation: 106

<script>
    var urlParams = new URLSearchParams(window.location.search);
    console.log(urlParams.toString()); // this will log the part you need: urlParams.toString()
</script>

Upvotes: 0

Catalin Iancu
Catalin Iancu

Reputation: 722

Do you actually need the whole part after the last /? Because you can get the url parameters (after ?) easy with window.location.search Otherwise you need a string manipulation function

Upvotes: 0

ksav
ksav

Reputation: 20830

window.location.href.split('/').reverse()[0]

Upvotes: 3

David
David

Reputation: 529

You can get the window.location and extract this part

console.log(window.location)

Upvotes: 0

Related Questions