0

Below is my GET request. I am trying to retrieve the client_id and redirect_uri parameters.

https://sdkapp.example.com:8443/central-login/index.html?client_id=dtvClient&redirect_uri=https://www.example3.com:443/callback

And then utilize those values, in a embedded js script within the same html page.

Config.set({
  clientId: //fetched query parameter for client_id
  redirectUri: // fetched query parameter for redirect_uri
  });
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Karan Nayyar
  • 666
  • 2
  • 7
  • 14
  • In the browser or on the server? Normally you'd be doing this on the server. If on the server, what technology are you using server-side? (Presumably not jQuery! :-) ) – T.J. Crowder Mar 10 '22 at 08:06
  • I am doing this on browser. The page is a SPA with embedded js. Using Vanilla JS only for retrieving this value from url. – Karan Nayyar Mar 10 '22 at 08:08
  • So why the [tag:jquery] tag? Most people who say "vanilla JavaScript" mean "without any libs like jQuery". – T.J. Crowder Mar 10 '22 at 08:09

1 Answers1

2

If this is on the client you can use URL and searchParams

// const url = new URL(location.href); // uncomment and delete next line
const url = new URL("https://sdkapp.example.com:8443/central-login/index.html?client_id=dtvClient&redirect_uri=https://www.example3.com:443/callback"); // for example

const obj = {
  "clientId": url.searchParams.get("client_id"),
  "redirectUri": url.searchParams.get("redirect_uri")
};
console.log(obj)

// Config.set(obj)

If on the server: for example node also has URL

And here is an answer for php: Get URL query string parameters

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 1
    I'd think getting the query string params in the browser would be an often-repeated duplicate. Since the OP has confirmed they do indeed mean the browser, [this](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) for instance. – T.J. Crowder Mar 10 '22 at 08:10
  • 1
    @T.J.Crowder I agree, but that specific dupe is SO FULL OF CODE - why on earth would one use a Proxy to call the URL api??? – mplungjan Mar 10 '22 at 08:12
  • 1
    There are other answers there. (I agree, the proxy thing is just dumb.) The point is, as you sometimes put it, "huge dupe" :-) – T.J. Crowder Mar 10 '22 at 08:14
  • I am receiving error on console like "Uncaught TypeError: Cannot read properties of undefined (reading 'get')" for the below line of code. var url_string =window.location.href; var client = url_string.searchParams.get("client_id"); – Karan Nayyar Mar 10 '22 at 08:18
  • @KaranNayyar That is because you are not using URL: `const url = new URL(window.location.href); const client = url.searchParams.get("client_id");` – mplungjan Mar 10 '22 at 08:20