简体   繁体   English

在Javascript中以任何方式知道从哪个主机脚本加载?

[英]In Javascript any way to know what host script was loaded from?

In javascript, as a script loaded from somer host, is there any way to know what server/host I was loaded from? 在javascript中,作为从somer主机加载的脚本,有什么方法可以知道我从哪个服务器/主机加载? I need to make additional ajax requests back to that host and would prefer to figure out the host dynamically. 我需要向该主机发送额外的ajax请求,并且更愿意动态地找出主机。

So if you include a javascript file on a page 因此,如果您在页面上包含javascript文件

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

when that javascript execute, within test.js ... 当javascript执行时,在test.js中......

var host_loaded_from = ??? // should be somehost.com 

Thanks 谢谢

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. 是的, 除非使用deferasync属性异步加载脚本,否则可能是这样,因为在这种情况下,DOM中的最后一个脚本可能不一定是当前正在执行的脚本。 See the comments by @kangax for more information. 有关更多信息,请参阅@kangax的评论。

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中,获取最后一个脚本元素,该元素将是当前正在解析的脚本(在您的情况下为test.js),并获取其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 ,使用正则表达式解析主机。

src.match(new RegExp('https?://[^/]*'))
["http://somehost.com"] // for your example

Nope, sorry. 不,谢谢。

If the <script> had an id, then maybe. 如果<script>有一个id,那么也许吧。 But you can't really rely on that. 但你不能真正依赖它。

Not sure if that can be done with JavaScript. 不确定是否可以使用JavaScript完成。 If the script is inside a PHP file do it like this: 如果脚本在PHP文件中,请执行以下操作:

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

On my site I include my JS scripts using PHP just for that reason. 在我的网站上,我出于这个原因使用PHP包含我的JS脚本。 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. 由于同源策略,您只能向源(主机+协议+端口)发出AJAX请求,从中加载HTML页面(文档) - 这不一定与加载js的源相同。

You can use window.location.hostname or document.location.hostname to find out the hostname of the document. 您可以使用window.location.hostnamedocument.location.hostname来查找document.location.hostname的主机名。

Does the script know its own file name? 脚本是否知道自己的文件名? ("test.js" in the OP question.) (OP问题中的“test.js”。)

If so, your script could query the dom for all script tags, looking for the ones with a src attribute. 如果是这样,您的脚本可以查询所有脚本标记的dom,查找具有src属性的脚本标记。 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: 这是JS在el中查找所有脚本标记:

var scripts = el.getElementsByTagName('SCRIPT');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM