Evan_HZY Evan_HZY - 3 years ago 96
AngularJS Question

How to extract a parameter from html fragment in JS/AngularJs?

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download