Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
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? -> stackoverflow.com/questions/6146632/… –  GNi33 Jul 17 '13 at 15:36
    
possible duplicate of Get the url of currently executing js file when dynamically loaded –  Bergi Jul 17 '13 at 15:37
add comment

2 Answers

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

share|improve this answer
    
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 <script src="danml.com/js/filename.js">< script><script>alert(__filename);</ script> from my desktop... (fix the tags from comment reformat) –  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
show 2 more comments

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.

share|improve this answer
    
No Jquery, onl javascript –  Devin Dixon Jul 17 '13 at 15:26
1  
this is not really related to the actual question –  GNi33 Jul 17 '13 at 15:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.