Getting Javascript Domain When Script Is From Another Domain -
i writing script suppose work across domains. trying include 1 script domain , have script include other scrips 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 window.location.host gives domain script has been download to: www.mydomain.com . wanted domain script resides on, in exmaple www.example.com ?
can done? , please no jquery.
here alternate method newer browsers works dom-adder-injected scripts, not last script in getelementsbytagname("script") collection:
(function(){ // script filename setter, leaves window.__filename set 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 ie8 (s+q), ff, ch
i don't know of different way, other manually sniffing script tag .srcs match hard-coded string, yuck.
quick demo: http://danml.com/pub/getfilenamedemo2.html linked script: http://danml.com/js/filename2.js
Comments
Post a Comment