简体   繁体   中英

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.

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

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>

You can get the window.location and extract this part

 console.log(window.location)

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

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

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)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM