12

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

mbrevoort
  • 5,075
  • 6
  • 38
  • 48

5 Answers5

12

is there any way to know what server/host I was loaded from?

Yes, it's possible except when the script is loaded asynchronously using defer or async attributes 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.

// test.js
var scripts = document.getElementsByTagName('script');
var src = scripts[scripts.length - 1].src;

One the src is found, parsed the host using regex.

src.match(new RegExp('https?://[^/]*'))
["http://somehost.com"] // for your example
Community
  • 1
  • 1
Anurag
  • 140,337
  • 36
  • 221
  • 257
  • Last script element is not necessarily the one that's currently being executed, though (think "defer" and "async" attributes), so this approach is not reliable. – kangax Aug 26 '10 at 20:28
  • @kangax - is it correct to say the above will work with the exception of when `defer` or `async` attributes are used? – Anurag Aug 26 '10 at 20:35
  • 2
    Yes, I think so. Current HTML5 spec — which, for the most part, should reflect real browsers behavior — says: "If neither attribute is present (meaning "defer" and "async"), then the script is fetched and executed immediately, before the user agent continues parsing the page." So currently executed script should in fact be last script element in the document (since parsing can not continue until execution is finished). – kangax Aug 26 '10 at 20:46
  • Why not simply add an `id` attribute to the script tag and use `document.getElementById('the-script-id')` to get the script element? – ovi Jun 25 '15 at 10:57
1

Does the script know its own file name? ("test.js" in the OP question.)

If so, your script could query the dom for all script tags, looking for the ones with a src attribute. But you'd need to watch out for two scripts with the same file name loaded from different servers. Eg

<script src="http://somehost.com/js/test.js"></script>
<script src="http://somehost_number2.com/js/test.js"></script>

Here's JS that looks for all of the script tags in an el:

var scripts = el.getElementsByTagName('SCRIPT');
Larry K
  • 47,808
  • 15
  • 87
  • 140
0

Nope, sorry.

If the <script> had an id, then maybe. But you can't really rely on that.

Ryan Kinal
  • 17,414
  • 6
  • 46
  • 63
0

Not sure if that can be done with JavaScript. If the script is inside a PHP file do it like this:

...
var host_loaded_from = <?php echo $_SERVER[SERVER_NAME] ?>
...

On my site I include my JS scripts using PHP just for that reason. I'm interested to see if there's a better way.

Mikey1980
  • 971
  • 4
  • 15
  • 24
0

Due to Same Origin Policy, you can make AJAX requests only to the origin (host + protocol + port), the HTML page (document) was loaded from - which is not necessarily the same as the origin your js was loaded from.

You can use window.location.hostname or document.location.hostname to find out the hostname of the document.

Chris Lercher
  • 37,264
  • 20
  • 99
  • 131
  • Remember while Ajax calls are limited, a Javascript script CAN load additional scripts, data (using JSONP), images, etc from any domain. The Same Origin Policy is about straight Ajax calls. – Larry K Aug 26 '10 at 20:04
  • 1
    @Larry: Yes, but the OP was asking specifically about Ajax calls. (JSONP is not Ajax.) – Chris Lercher Aug 26 '10 at 20:05