-2

In Javascript, how can I get the parameters of a URL string (not the current URL)?

http://localhost:8080/feasthunt/changePassword.html?TOKEN=0FA3267F-0C62-B1C9-DB71-76F6829671ED

can i get token in JSON object?

ASHOK
  • 17
  • 4

2 Answers2

0

try this

var str = "http://localhost:8080/feasthunt/changePassword.html?TOKEN=0FA3267F-0C62-B1C9-DB71-76F6829671ED";
var tokenValue = str.substring(str.indexOf("?")+1).split("=")[1];

Or more generic

var paramMap = {}; str.substring(str.indexOf("?")+1).split("&").forEach(function(val){
  var param = val.split("=");
  paramMap[param[0]] = param[1];
})

paramMap is your JSON object, where paramMap["TOKEN"] will give you the value for this param

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

No need for a 'JSON' object, and just use split to grab it, since its after a '='

var url = 'http://localhost:8080/feasthunt/changePassword.html?     TOKEN=0FA3267F-0C62-B1C9-DB71-76F6829671ED';
var token = url.split('=').pop(); 

//token is equal to: "0FA3267F-0C62-B1C9-DB71-76F6829671ED"

https://jsbin.com/siyazo/1/edit?js,console

omarjmh
  • 13,632
  • 6
  • 34
  • 42