最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

In Javascript any way to know what host script was loaded from? - Stack Overflow

programmeradmin3浏览0评论

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=".js"></script>

when that javascript execute, within test.js ...

var host_loaded_from = ??? // should be somehost 

Thanks

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

Share Improve this question asked Aug 26, 2010 at 19:50 mbrevoortmbrevoort 5,1656 gold badges41 silver badges48 bronze badges 1
  • 1 +1 good question, interested in seeing if it's possible using JS – Mikey1980 Commented Aug 26, 2010 at 20:02
Add a comment  | 

5 Answers 5

Reset to default 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

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');

Nope, sorry.

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

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.

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.

发布评论

评论列表(0)

  1. 暂无评论