SpencerDevv
SpencerDevv

Reputation: 13

I need to get a URL for data (html)

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

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

If you can use JavaScript, you can use the following code to grab the current URL and parse the part you need!

  1. The location.href will give you the complete URL.
  2. The location.search will give you the params: ?key=Player_1637114231
  3. Now remove the ?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

Related Questions