4

Is there any way that in an external javascript file, can know the host of the file?

For example, if I have the site http://hostOne.com/index.php, the code of the file index.php:

<html>
<head>
   <script type="text/javascript" src="http://hostTwo.com/script/test.js"></script>
</head>
<body>
   <div>...</div>
</body>
</html>

I need that in the file test.js can know the host http://hostTwo.com.

Thank you.

EDIT

or it can know the tag "script" which was called?, with this option I can analyzes the tag and get the "src" attribute. But I don't want to depend on the name of the file test.js and analyze all the tag script that contains the site.

*Solution based on the code of @Armi *

Html:

    <html>
    <head>
      <script class="jsbin"  src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
      <script id="idscript" type="text/javascript" src="http://hostTwo.com/script/test.js"></script>
    </head>
    <body>
       <div>...</div>
    </body>
    </html>

code in JS

var 
     url = $('head').find('#idscript').attr('src'),
     host = url.replace(/(\/\/.*?\/).*/g, '$1');

console.log(host);
andres descalzo
  • 14,887
  • 13
  • 64
  • 115

4 Answers4

1

I've got an idea (the snippet based on jQuery):

var yourScriptTag = $('head').find('script[src$="jquery-1.7.1.js"]').eq(0);
var theHostnameOfYourScript = $(yourScriptTag).attr('src').replace(/(http:\/\/.*?\/).*/g, '$1');

alert(theHostnameOfYourScript);

jsfiddle example: http://alpha.jsfiddle.net/XsJn8/

If you know the filename of your script (and if this is always the same and unique) you can use this snippet to get the hostname.

If this path is relative (and contains no host) you can get the hostname with a simple location.hostname

Armin
  • 15,582
  • 10
  • 47
  • 64
  • Yes, but I'm going to look for the ID of the script instead of the name of the script `$('head').find('#idcript').attr('src').replace(/(\/\/.*?\/).*/g, '$1')` http://jsbin.com/ovosow/edit#source – andres descalzo Feb 09 '12 at 13:36
  • Of course, if you define to use this id, this is possible! But it should be a unique id to avoid conflicts with existing ones :) – Armin Feb 09 '12 at 13:38
0

Sorry, not possible. The content of the script is downloaded and after this it is fired. At this point the script "thinks" he is at your site.

Of course unless the host is hardcoded in the script.

freakish
  • 54,167
  • 9
  • 132
  • 169
0

This is not possible, because the JavaScript code is executed client-sided. You could propably parse it somehow out of your URL but, I don't think either that this is very useful and possible.

mas-designs
  • 7,498
  • 1
  • 31
  • 56
-1

Inside test.js, you can use :

var url = document.URL;

then parse the url result.

You can't make cross-site scripting, so if you need more sophisticated stuff, you could write your javascript in php and call :

<script type="text/javascript" src="http://hostTwo.com/script/test.php"></script>

But that's not standard. Anyway,, the solution is on the server, with a designed proxy.

Nicolas Zozol
  • 6,910
  • 3
  • 50
  • 74