Get Parameter from URL in JavaScript that starts with # and not ?


Joined
Apr 29, 2021
Messages
4
Reaction score
0
Hello! My name is Donovan. I’m using the Yandex OAuth API to check if they have an email at @mail.(my domain) through Yandex360.

The problem: I need to extract the token from this URL.
https://mail.(my domain)/auth/server.php#access_token=TOKENGOESHERE&token_type=TYPE&expires_in=EXPIRATION

I want to get TOKENGOESHERE from the URL to connect to the Yandex API to check the email on the account. How can I get TOKENGOESHERE from the URL? (I originally asked this question on a PHP fourm and they said I had to know JavaScript, so I'm asking here.)

EDIT--Changed PHP to JavaScript.
 
Last edited:
Ad

Advertisements

Joined
Nov 27, 2019
Messages
163
Reaction score
24
Answer both easy and hard. I wish JS had a method to do this a lot simpler than what it takes.
If you want an explanation just ask.
Code:
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<div id="demo"></div>
<script>
  var str = "https://mail.(my domain)/auth/server.php#access_token=TOKENGOESHERE&token_type=TYPE&expires_in=EXPIRATION";
  var res = str.split("=");
  var first = res[1];
  var sec = first.split("&");
  var ans = sec[0];  //THIS IS WHAT YOU WANT
document.getElementById("demo").innerHTML = ans;  //THIS IS A LINE TO SHOW IT IN THE HTML CODE - YOU DON'T NEED THIS
</script>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top