I know this is probably extremely simple, but I can't seem to figure it out or find the answer I'm looking for. I'm using Instagram's API to allow user's to login and see their feed. This is done on the client side with Javascript. After authorizing my app, the browser sends back an access token in the url like so: www.example.com/#access_token=12345679
.
What's the simpest vanilla JS to get the raw number of the access token? I've tried location.hash
but that returns both the key and value like so: acess_token=123456789
Any help appreciated.
To get hash parameters from request URL with JavaScript, we can use the URL instance’s hash property. to create a new URL instance from a URL string. Then we get the hash value of the URL from the hash property. Therefore, hash is '#xyz'.
Using JavaScript you can easily get the value after the hashtag (#) from the URL, there are no need to use jQuery. Also, you can get the hash value from href using simple JavaScript code. The following JavaScript code will show how to get value with, after and before hashtag (#). var hash = location.hash;
The URLSearchParams is an interface used to provide methods that can be used to work with an URL. The URL string is first separated to get only the parameters portion of the URL. The split () method is used on the given URL with the “?” separator. It will separate the string into 2 parts.
Many times we modify the web page elements depending on the hash (#) value of the current URL. It’s very effective way to pass a value with the URL and load the web page based on that value. Using JavaScript you can easily get the value after the hashtag (#) from the URL, there are no need to use jQuery.
Assuming the hash pattern is consistent, you can get the access_token value with the following code:
var hash = window.location.hash;
var accessToken = hash.split('=')[1];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With