I have a script that is using ajax to consume an api. My current script looks for a script tag with an id of "test-api" and passes the data attribute (an id) to the ajax call to complete the url for lookup.
<script src="./bundle/src/js/api.js" id="test-api" data-client="2"></script>
Inside my script:
var clientId = $('#test-api').data('client');
//short hand
axios.get('http://url_to_api.com/clients' + clientID)
.then()
All of this works quite well, but I have now run into an issue. This script might be duplicated on the same page (as I might need to pull in other "clients") so I need to have ids for all of the scripts.
<script src="./bundle/src/js/api.js" id="test-api" data-client="2"></script>
<script src="./bundle/src/js/api.js" id="test-two-api" data-client="5"></script>
<script src="./bundle/src/js/api.js" id="test-three-api" data-client="6"></script>
I tried using
var script = document.currentScript;
script = script[script.length - 1]
but I keep getting null returned (also regarding this, my script is being placed via a gtm, so it is not always the last script). As I understand, it is null due to this being placed in an .then() function (running after another script finishes).
I guess my question is it possible to actually get the id of the current script without really know what ID is present (not hardcoded into the script)? Unfortunately, I do need to support IE as well so I assume .currentScript is out of the question as well