Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get name of JavaScript file being executed?
    text
    copied!<p>I'm working on a project that contains several features, so I'm trying to organize each feature on a separated JavaScript file in such a way that the code can be loaded on demand. I want to standardize the load and execution process so I'm trying to develop a function that can load and execute the JavaScript file if it is not present in memory or just execute it if it is already loaded. One approach would be to make the function to require two parameters, the file name and the name of entry point function, then check if the script is already loaded, if it is just execute the entry point function, or if it is not, then load the script and upon the onload event execute the entry point function, this approach works fine but it requires the main module to know all the entry point function names, which in the future may become a pain in the neck, as a careless programmer may inadvertently duplicate a function name. A much cleaner and safer approach would be to name "main" all the entry point functions, and upon first execution associate that "main" to its script DOM object... more or less like this:</p> <p>Loaded module:</p> <pre><code>function(){ function main(){ . . some code here . } Install(getThisFileName(),main); }(); </code></pre> <p>Main module:</p> <pre><code>function Load(fn){ var el,ff,i el = document.getElementsByTagName("script"); ff = "http://"+window.location.host+fn; for(i=el.length-1;i&gt;=0;i--){ if(el[i] &amp;&amp; el[i].src==ff){ el[i].Main(); return; } } el = document.createElement("script"); el.type = "text/javascript"; el.src = ff; } function Install(ff,ep){ var el,i; el = document.getElementsByTagName("script"); for(i=el.length-1;i&gt;=0;i--){ if(el[i] &amp;&amp; el[i].src==ff){ el[i].Main = ep; ep(); return; } } } </code></pre> <p>But in order to do so I need to know the name of the JavaScript file being executed, which I don't know if it is even possible. Any Ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload