get external script url from within
once upon a time, we were able to determine the url of our scripts with a small snip:
once document.createElement("script") - based loaders entered the picture, that capability became unreliable.
you could iterate all the script tags and try to match up your src, but if the files get renamed, the code breaks, causing maintenance headaches.
i figured out a neat way to use the error object to infer the executing script's URL.
While it doesn't work in past versions of IE, it DOES work on mobile, FF, Chrome, and IE 10, so it should be great going forward and in the mobile/tablet space today.
unlike guessing and checking urls, this method requires no per-edit or per-file customization.
Note that it returns the name of the file that contains the __filename() function, not the name of the file that calls it.
This makes it especially nice for checking-in modules, since you can simply attach it as a method to exports, or whatever your module's namespace happens to be. You might just want to run it once and save the string URL as a property on the module, a constructor, or something else, it's super-simple to use in many ways.
The function returns "http://undefined" if pasted into a firebug/inspector console (for reasons i hope are obvious to you) but i made a simple online demo if you want to "kick the tires".
|All times are GMT +1. The time now is 06:55 PM.|
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.