Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

location.host vs location.hostname and cross-browser compatibility?

Which one of these is the most effective vs checking if the user agent is accessing via the correct domain.

We would like to show a small js based 'top bar' style warning if they are accessing the domain using some sort of web proxy (as it tends to break the js).

We were thinking about using the following:

var r = /.*domain\.com$/; if (r.test(location.hostname)) {     // showMessage ... } 

That would take care of any subdomains we ever use.

Which should we use host or hostname?

In Firefox 5 and Chrome 12:

console.log(location.host); console.log(location.hostname); 

.. shows the same for both.

Is that because the port isn't actually in the address bar?

W3Schools says host contains the port.

Should location.host/hostname be validated or can we be pretty certain in IE6+ and all the others it will exist?

like image 854
anonymous-one Avatar asked Jul 17 '11 18:07

anonymous-one


People also ask

What is location host?

The Location Host property in HTML is used to sets or returns the hostname and port of a URL. The Location Hash property doesn't return the port number if it is not specified in the URL. Syntax: It returns the host property.

What does Window location hostname return?

Window Location Hostname hostname property returns the name of the internet host (of the current page).

What does Window location replace do?

Window location. The replace() method replaces the current document with a new one.


1 Answers

interactive link anatomy

As a little memo: the interactive link anatomy

--

In short (assuming a location of http://example.org:8888/foo/bar#bang):

  • hostname gives you example.org
  • host gives you example.org:8888
like image 74
abernier Avatar answered Oct 06 '22 13:10

abernier