Reputation: 491
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
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
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)
Upvotes: 0
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
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
Reputation: 529
You can get the window.location and extract this part
console.log(window.location)
Upvotes: 0