Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

javascript - location.host与location.hostname和跨浏览器的兼容性?(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).

(如果他们使用某种Web代理访问域名(因为它倾向于打破js),我们希望显示一个基于小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:

(在Firefox 5和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.

(W3Schools说主机包含端口。)

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

(应该验证location.host/hostname还是我们可以在IE6 +中确定其他所有其他内容?)

  ask by anonymous-one translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

互动链接解剖学

As a little memo: the interactive link anatomy

(作为一个小备忘录: 交互式链接解剖)

--

( - )

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

(简而言之(假设http://example.org:8888/foo/bar#bang的位置):)

  • hostname gives you example.org

    (hostname为您提供example.org)

  • host gives you example.org:8888

    (host给你example.org:8888)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...