0

I'd like to know if it is possible to write a bit of javascript in a web page to know if the web-page is being hosted remotely or not.

Suppose I go and modify the hosts file and I write 127.0.0.1 www.myexample.com

then I can run locally a webserver and display my test.html page

http://www.myexample.com/test.html

The question is: "can I figure out that I am running locally...?' alternatively can I resolve the www.myexample.com into its address (but I think that's more complicated)

Zo72
  • 14,593
  • 17
  • 71
  • 103

3 Answers3

3

No, this cannot be done with JavaScript on the browser side.

If you put example.com in your hosts file JavaScript doesn't know if example.com is running locally or if it's from another server for example.

Similar questions:

Determine IP# of domain from client browser
http://answers.yahoo.com/question/index?qid=20110606071036AAztRkF
http://javascript.about.com/library/blip.htm

Community
  • 1
  • 1
alessioalex
  • 62,577
  • 16
  • 155
  • 122
  • I assume you are correct. Can you edit your answer so to prove it. Can you add links to resources that confirm that this can not be done. Please do so and I will mark your answer as the correct one. I just need some validation really since I can not prove it myself – Zo72 Dec 09 '11 at 08:56
  • I edited my answer with some links, other than that I couldn't find you a more 'official' answer. – alessioalex Dec 09 '11 at 09:03
0

You could approximate. Just make an Ajax call for some static resources and mesaure the time u need. If its below e.g. 5ms it should be local.

Taner Topal
  • 921
  • 8
  • 14
0

you need to look at

window.location.host

this shows the host the page is loaded on

Richard
  • 21,728
  • 13
  • 62
  • 101
  • If aliased in the host file per the question, won't it just show the aliased hostname rather than localhost or 127.0.0.1? I think this would only work if the IP itself is being actively used. – Greg Pettit Dec 08 '11 at 14:37