Javascript文件中的代码可以知道自己的域/ URL [英] Can code in a Javascript file know its own domain/URL
问题描述
如果我有一个站点,example.com和该站点上的页面引用了subdomain.example.com/serveAd.js上的Javascript - 是否有一种方法可以在serveAd.js中知道自己的URL,或者它是从哪个域下载的?
If I have a site, example.com, and a page on that site references a Javascript at subdomain.example.com/serveAd.js -- is there a way from within serveAd.js to know its own URL, or the domain from which it was downloaded?
(JS当然可以知道调用它的页面。我想知道JS是否知道它自己的来源。 )
(The JS can certainly know all about the page that called it. I want to know if the JS knows about its own origin.)
推荐答案
如果你使用的是jquery,你可以通过这种方式获得完整的URL:
If you were using jquery, you could get the full url with this kind of thing:
var fullJsUrl= $('script[src$=serveAd.js]').attr('src');
如果不使用jquery,可能需要更多工作,但你明白了;)
If not using jquery, it may take more work, but you get the idea ;)
这篇关于Javascript文件中的代码可以知道自己的域/ URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!