[英]Getting Javascript Domain When Script Is From Another Domain
I am writing a script that is suppose to work across domains.我正在编写一个可以跨域工作的脚本。 I trying to include one script from another domain and then have that script include other scrips from the same domain
我试图包含来自另一个域的一个脚本,然后让该脚本包含来自同一域的其他脚本
Example: Domain 1 - www.mydomain.com示例:域 1 - www.mydomain.com
<html>
<head>
<script type="text/javascript" src="http://www.example.com/app.js"></script>
</head>
</html>
Example App JS示例应用程序 JS
var imported = document.createElement('script');
imported.src = window.location.host + 'config.js';
document.head.appendChild(imported);
var imported = document.createElement('script');
imported.src = window.location.host + 'stuff.js';
document.head.appendChild(imported);
The problem is window.location.host gives the domain that the script has been download to: www.mydomain.com .问题是 window.location.host 给出了脚本已下载到的域: www.mydomain.com 。 I wanted the domain that the script currently resides on, which is in this exmaple is www.example.com ?
我想要脚本当前所在的域,在这个例子中是 www.example.com 吗?
Can this be done?这能做到吗? And please no JQuery.
请不要使用 JQuery。
You can try this你可以试试这个
if (document.currentScript && document.currentScript.src) {
var uri = new URL(document.currentScript.src);
includeJsFile(uri.origin + '/static/script.js');
}
Please note that document.currentScript does not work on IE.请注意 document.currentScript 不适用于 IE。
https://caniuse.com/#search=document.currentScript https://caniuse.com/#search=document.currentScript
By reading your question again i would simply ask:通过再次阅读您的问题,我会简单地问:
why dont you do that relatively to that js, say:你为什么不这样做相对于那个 js,说:
imported.src = 'config.js';
it will import the config.js that suppose to be a brother of app.js它将导入假设为 app.js 兄弟的 config.js
regarding to the comments i apologise for mistake didnt understood the question.关于评论我为错误道歉没有理解这个问题。
here is an alternate method for newer browsers that works even with dom-adder-injected scripts, which sometimes are NOT the last script in a getElementsByTagName("script") collection:这是一种适用于较新浏览器的替代方法,即使使用 dom-adder-injected 脚本也能工作,这些脚本有时不是 getElementsByTagName("script") 集合中的最后一个脚本:
(function(){ // script filename setter, leaves window.__filename set with active script URL.
if(self.attachEvent){
function fn(e,u){self.__filename=u;}
attachEvent("onerror",fn);
setTimeout(function(){detachEvent("onerror", fn)},20);
eval("gehjkrgh3489c()");
}else{
Object.defineProperty( window, "__filename", { configurable: true, get:function __filename(){
try{document.s0m3741ng()}catch(y){
return "http://" +
String(y.fileName || y.file || y.stack || y + '')
.split(/:\d+:\d+/)[0].split("http://")[1];
}
}})//end __filename
}//end if old IE?
}());
UPDATE: more comprehensive support added: TESTED IN IE9(s+q), IE9 as IE8 (s+q), FF, Ch更新:添加了更全面的支持:在 IE9(s+q)、IE9 中测试为 IE8 (s+q)、FF、Ch
i don't know of a different way, other than manually sniffing script tag .SRCs to match a hard-coded string, yuck.我不知道有什么不同的方法,除了手动嗅探脚本标签 .SRCs 以匹配硬编码字符串,哎呀。
quick demo: http://danml.com/pub/getfilenamedemo2.html linked script: http://danml.com/js/filename2.js快速演示: http : //danml.com/pub/getfilenamedemo2.html链接脚本: http : //danml.com/js/filename2.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.