4

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

<html>
    <head>
       <script type="text/javascript" src="http://www.example.com/app.js"></script>
    </head>
</html>

Example App 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 . I wanted the domain that the script currently resides on, which is in this exmaple is www.example.com ?

Can this be done? And please no JQuery.

Devin Dixon
  • 11,553
  • 24
  • 86
  • 167
  • `window` refers to the page that the script is currently in. You'd have to make the server dynamically print the host's URL in the JavaScript file and serve it up that way – Ian Jul 17 '13 at 15:23
  • Basically, you'd need to look for the src-string in your page and parse the actual domain. maybe this could help you out? -> http://stackoverflow.com/questions/6146632/get-domain-of-javascript-file – GNi33 Jul 17 '13 at 15:36
  • possible duplicate of [Get the url of currently executing js file when dynamically loaded](http://stackoverflow.com/questions/2277978/get-the-url-of-currently-executing-js-file-when-dynamically-loaded) – Bergi Jul 17 '13 at 15:37

3 Answers3

3

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.

https://caniuse.com/#search=document.currentScript

Khachatur
  • 921
  • 1
  • 12
  • 30
-1

By reading your question again i would simply ask:

why dont you do that relatively to that js, say:

imported.src = 'config.js';

it will import the config.js that suppose to be a brother of app.js

regarding to the comments i apologise for mistake didnt understood the question.

JohnnyJS
  • 1,320
  • 10
  • 21
-1

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:

(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

i don't know of a different way, other than manually sniffing script tag .SRCs to match a hard-coded string, yuck.

quick demo: http://danml.com/pub/getfilenamedemo2.html linked script: http://danml.com/js/filename2.js

dandavis
  • 16,370
  • 5
  • 40
  • 36
  • This is a great idea, but doesn't seem to work on anything. I just tried in old IE (even IE 9) - `fileName`, `file`, and `stack` aren't defined (like you warned, except IE 9). I tried it in Chrome (and IE 10) and only `stack` is defined, but the retrieval of the URL doesn't work. And in Firefox, all 3 are defined, but the retrieval doesn't work. I'm wondering if I'm doing something wrong... – Ian Jul 17 '13 at 19:17
  • @ian: are you calling it from an external script? – dandavis Jul 17 '13 at 19:21
  • Yeah, it's in a `test.js` file. I wonder if it matters that the external file is in the same project/domain – Ian Jul 17 '13 at 19:22
  • i just verified – dandavis Jul 17 '13 at 19:26
  • Hmm weird, I always get `http:/`. I'll keep messing around :) – Ian Jul 17 '13 at 19:29
  • try http://danml.com/pub/getfilenamedemo.html . works for me in CH+FF, but you're right about IE9, dang. maybe there's another property i can find. thanks for the heads-up. – dandavis Jul 17 '13 at 19:32
  • @Ian: ok, it's not as sexy, but IE9 std+qrks now seem to work: http://danml.com/pub/getfilenamedemo2.html – dandavis Jul 17 '13 at 20:03