How to get name of JavaScript file being executed? -


i'm working on project contains several features, i'm trying organize each feature on separated javascript file in such way code can loaded on demand. want standardize load , execution process i'm trying develop function can load , execute javascript file if not present in memory or execute if loaded. 1 approach make function require 2 parameters, file name , name of entry point function, check if script loaded, if execute entry point function, or if not, load script , upon onload event execute entry point function, approach works fine requires main module know entry point function names, in future may become pain in neck, careless programmer may inadvertently duplicate function name. cleaner , safer approach name "main" entry point functions, , upon first execution associate "main" script dom object... more or less this:

loaded module:

function(){    function main(){       .       . code here       .    }    install(getthisfilename(),main); }(); 

main module:

function load(fn){ var el,ff,i    el = document.getelementsbytagname("script");    ff = "http://"+window.location.host+fn;    for(i=el.length-1;i>=0;i--){       if(el[i] && 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>=0;i--){       if(el[i] && el[i].src==ff){          el[i].main = ep;          ep();          return;       }    } } 

but in order need know name of javascript file being executed, don't know if possible. ideas?

the easiest way might have included first:

var currentfile; function getthisfilename() {   return currentfile; } 

and @ beginning of each file @ top do:

currentfile = 'thisfilename.js'; 

this work if code executed sequentially, once files loaded currentfile name of last file.

and alternative method set currentfile @ start of each function in each file. while specific function being accessed can call getthisfilename() , tell last file run, or current file being run.

unfortunately has bit of overhead code itself.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -