For example, if I have the site
http://hostOne.com/index.php, the code of the file
I need that in the file
test.js can know the host
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 *
code in JS
var url = $('head').find('#idscript').attr('src'), host = url.replace(/(\/\/.*?\/).*/g, '$1'); console.log(host);
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