Home > front end >  How can I convert a URL which is in String datatype to a Route object?
How can I convert a URL which is in String datatype to a Route object?

Time:12-27

I have a URL stored in a string.

const URL = "http://localhost:8080/employee?name=john"

I want to convert this URL to route object so that I can get the name(query param) from the route object.

Is there a way to do that?

CodePudding user response:

Create an URL object from that string, then use searchParams.get to get the param value :

const urlString = "http://localhost:8080/employee?name=john"

const url = new URL(urlString)

const name = url.searchParams.get('name'); //gives 'john'
  • Related