How can I reference the script element that loaded the javascript that is currently running?
Here's the situation. I have a "master" script being loaded high in the page, first thing under the HEAD tag.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript" src="scripts.js"></script>
There is a script in "scripts.js" which needs to be able to do on-demand loading of other scripts. The normal method doesn't quite work for me because I need to add new scripts without referencing the HEAD tag, because the HEAD element hasn't finished rendering:
document.getElementsByTagName('head')[0].appendChild(v);
What I want to do is reference the script element that loaded the current script so that I can then append my new dynamically loaded script tags into the DOM after it.
<script type="text/javascript" src="scripts.js"></script>
loaded by scripts.js--><script type="text/javascript" src="new_script1.js"></script>
loaded by scripts.js --><script type="text/javascript" src="new_script2.js"></script>
Best Solution
How to get the current script element:
1. Use
document.currentScript
document.currentScript
will return the<script>
element whose script is currently being processed.Benefits
defer
&async
)Problems
<script type="module">
2. Select script by id
Giving the script an id attribute will let you easily select it by id from within using
document.getElementById()
.Benefits
defer
&async
)Problems
id
attribute may cause weird behaviour for scripts in some browsers for some edge cases3. Select the script using a
data-*
attributeGiving the script a
data-*
attribute will let you easily select it from within.This has few benefits over the previous option.
Benefits
defer
&async
)Problems
querySelector()
not compliant in all browsersid
attribute<script>
withid
edge cases.4. Select the script by src
Instead of using the data attributes, you can use the selector to choose the script by source:
In embed.js:
Benefits
defer
&async
)Problems
id
attribute5. Loop over all scripts to find the one you want
We can also loop over every script element and check each individually to select the one we want:
This lets us use both previous techniques in older browsers that don't support
querySelector()
well with attributes. For example:This inherits the benefits and problems of whatever approach is taken, but does not rely on
querySelector()
so will work in older browsers.6. Get the last executed script
Since the scripts are executed sequentially, the last script element will very often be the currently running script:
Benefits
Problems
defer
&async
)