How can a javascript file know where it is located? For example:
<script type="text/javascript" src="http://mysite.com/scripts/howdy.js"></script>
How can the code in howdy.js know about http://mysite.com/scripts/howdy.js?
Edit: Clarification. I cannot rely on searching the DOM for my script tag because I need to know about it before the DOM may be ready.
In the moment in which the current script is being executed, will be the last
scriptelement in the DOM, therefore you can get it by:Check this example that loads this script.
Edit: Taking in consideration the @kangax’s comment, about the
asyncanddeferattributes, the only safe way IMO, previously knowing the file name, would be to inspect thescriptelements on the page, examining itssrcattribute, some libraries like Scriptaculous.us use this technique, for example: