0

I am trying to read the query parameter in javascript file.

Code-

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Chat App List</title>
</head>
<body>
  <div id="app"></div>
  <script src="bundle.js?cid=vivek"></script>   // I am trying to read value of cid in bundle.js file
</body>
</html>

So far , i tried using window.location.href in bundle file but it is giving undefined because it reads from url and the url serves index.html file

NeiL
  • 791
  • 8
  • 35
  • Which query parameter? – d4rty May 13 '16 at 07:14
  • 1
    check this http://stackoverflow.com/questions/4716612/how-do-i-get-query-string-value-from-script-path – GBnewbie May 13 '16 at 07:15
  • try getting the reference of the `script` tag and access its `src` property – mr5 May 13 '16 at 07:15
  • Is there a reason you're trying to pass data through a query string in that way? It seems like there may be a better solution for your problem. – sheeldotme May 13 '16 at 07:25
  • @sheeldotme bundle.js will not exactly is a static file, it is a cdn server path which is in sync with another node server, which will serve the dynamic js file after reading cid, but that cid is also need in the actual js content which server will return. – NeiL May 13 '16 at 07:39
  • @NeiL if the file is dynamically generated can you not include the cid in the generation? – sheeldotme May 13 '16 at 07:45

1 Answers1

0

windows.location.href is going to give you the address that you see in the address bar of the browser.

although you could possibly parse the DOM and find out what you need, but there's certainly no recommended way you're going to get that query param in your code.

EDIT: found same question asked earlier - Get the query string on the called javascript file

Community
  • 1
  • 1
dpkg
  • 181
  • 9
  • window.localtion.href will not work because file is server from index.html. I have read it from DOM – NeiL May 13 '16 at 07:41