1

There is an url like this:

http://domain/home#id_token=token123&state=staticState

How can I extract the "id_token" fragment (id_token=token123) and it's value(token123) in JS or AngularJs?

Thanks in advance :)

2 Answers 2

1

You can use URL constructor, .hash property of returned object, .split() with parameter "&", .shift() to get portion of string before "&" character

const url = "http://domain/home#id_token=token123&state=staticState";

const params = new URL(url).hash.split("&").shift();

console.log(params);

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome!! Thanks a lot :)
1

How can I extract the "id_token" fragment (id_token=token123) and it's value (token123) ?

var url = "http://domain/home#id_token=token123&state=staticState";
var queryString = url.split('#')[1]
var queryParts = queryString.split('&');
var queryObj = queryParts.reduce((obj,q) => {
   var key = q.split('=')[0];
   var val = q.split('=')[1];
   obj[key]=val;
   return obj;
},{});
console.log("query object =>", queryObj);
console.log("id_token=" + queryObj['id_token']);
console.log(queryObj['id_token']);

The above example separates the query string from the url. It then creates an object that has keys and values parsed from the query string. Individual parameter values can be fetched using property accessor bracket notation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.