In javascript, as a script loaded from somer host, is there any way to know what server/host I was loaded from? I need to make additional ajax requests back to that host and would prefer to figure out the host dynamically.
So if you include a javascript file on a page
<script src="http://somehost.com/js/test.js"></script>
when that javascript execute, within test.js …
var host_loaded_from = ??? // should be somehost.com
Thanks
Yes, it’s possible except when the script is loaded asynchronously using
deferorasyncattributes since the last script in the DOM may not necessarily be the currently executing script in that case. See the comments by @kangax for more information.Also, this same question was posted recently.
Inside your test.js, get the last script element which will be the currently being parsed script (test.js in your case), and get its
src.One the
srcis found, parsed the host using regex.