Reputation: 13
I am looking to get the Player ID at the end of a URL that looks exactly in this format, most of the time:
website.domain/get?key=Player_1637114231
I need to get the ID at the end (1637114231
). How would I do this in my HTML page?
Upvotes: 0
Views: 262
Reputation: 167182
If you can use JavaScript, you can use the following code to grab the current URL and parse the part you need!
location.href
will give you the complete URL.location.search
will give you the params: ?key=Player_1637114231
?key=Player_
so that you can get the desired output with added +
for integer conversion.Here's the code you need:
const id = +location.search.replace("?key=Player_", "");
This is assuming that your URL is always ?key=Player_XXXX
, where XXXX
is any number of any length and not any other parameters.
Here's a demo:
// On your page, use:
// const { search } = location;
const search = "?key=Player_1637114231";
console.log(+search.replace("?key=Player_", ""));
Upvotes: 1