0

This is what I want to achieve.

Suppose I have the html file with the following tag in it

<script src="https://beta.foo.com/test.js"></script>

I need a function which I can can call from inside the test.js file, to get the URL 'https://beta.foo.com/test.js'

If the html file which loaded the js file used the following tag to pull the js file

<script src="https://production.foo.com/test.js"></script>

Then the function should give me 'https://production.foo.com/test.js', if called from inside test.js

The reason I need this, is because of the effort I have to put in, while deploying my code to production. The test.js file uses is an API, with quite a few AJAX calls, which have to switch from beta to prodction as per which server they are called from.

I have already made it such that I can change this from a single variable, but then I would like it if I don't have to touch that variable during deployment of my code.

Nitro
  • 1,063
  • 1
  • 7
  • 17
  • 1
    Why not charging another js file on top, initializing a single trigger variable ? Then, you dont have to touch the file test.js itself, just load a file like "param.js" on top of it – iguypouf Jul 30 '19 at 05:33
  • You can try something with `document.querySelectorAll('script').forEach(item=>console.log(item.src))` after the document has been loaded – FrV Jul 30 '19 at 05:34
  • 1
    https://stackoverflow.com/questions/710957/how-might-i-get-the-script-filename-from-within-that-script – melpomene Jul 30 '19 at 05:37
  • @iguypouf: The js file will also be used by customers as API, so I can't ask them to do that. – Nitro Jul 30 '19 at 08:15
  • @Nitro: if you charge a file param.js on top only on your test server with a specific var, the var will be undefined on production, so you know you are in production :) – iguypouf Jul 31 '19 at 09:36

0 Answers0