Friday, 9 August 2013

Get host from where script has been requested

Get host from where script has been requested

I have a script that other people request on their websites from my host.
I have multiple environments (dev/test/production), and within my script,
I want to know the environment from which it has been loaded.
For example, I'm serving script.js at
dev.mysite.com/script.js
test.mysite.com/script.js
www.mysite.com/script.js
Someone loads my script as:
<script src="dev.mysite.com/script.js"></script>
So within script.js, I want something like:
var getHost = function() {
var host = // host from where it has been requested
return host; // in this case it should return dev.mysite.com
};
How can I accomplish this?

No comments:

Post a Comment