5

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.

3
  • 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 Commented Jul 17, 2013 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? -> stackoverflow.com/questions/6146632/… Commented Jul 17, 2013 at 15:36
  • possible duplicate of Get the url of currently executing js file when dynamically loaded Commented Jul 17, 2013 at 15:37

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

-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.

2 Comments

No Jquery, onl javascript
this is not really related to the actual question
-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

7 Comments

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: are you calling it from an external script?
Yeah, it's in a test.js file. I wonder if it matters that the external file is in the same project/domain
i just verified <script src="danml.com/js/filename.js">< script><script>alert(__filename);</ script> from my desktop... (fix the tags from comment reformat)
Hmm weird, I always get http:/. I'll keep messing around :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.