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 :)
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);
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.