Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get param value after hash in URL in vanilla JS

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.

like image 564
Sean Thompson Avatar asked Dec 09 '13 22:12

Sean Thompson


People also ask

How to get hash parameters from request URL with JavaScript?

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'.

How to get hash value from href in JavaScript?

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;

What is the use of urlsearchparams in Java?

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.

How to get the value after the hashtag (#) from the URL?

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.


1 Answers

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];
like image 196
Adam Zamozniewicz Avatar answered Nov 06 '22 18:11

Adam Zamozniewicz