gpt4 book ai didi

javascript - location.host vs location.hostname 和跨浏览器兼容性?

转载 作者:bug小助手 更新时间:2023-10-28 10:55:21 31 4
gpt4 key购买 nike

与检查用户代理是否通过正确的域进行访问相比,哪一项最有效。

如果他们正在使用某种网络代理访问域(因为它往往会破坏 js),我们希望显示一个基于 js 的小型“顶栏”样式警告。

我们正在考虑使用以下内容:

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

这将处理我们曾经使用的任何子域。

我们应该使用主机还是主机名?

在 Firefox 5 和 Chrome 12 中:

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

.. 两者显示相同。

那是因为端口实际上不在地址栏中吗?

W3Schools表示主机包含端口。

是否应该验证 location.host/hostname 或者我们可以非常确定在 IE6+ 和所有其他它将存在?

最佳答案

interactive link anatomy

作为一个小备忘录:interactive link anatomy

--

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

  • hostname 为您提供 example.org
  • host 为您提供 example.org:8888

关于javascript - location.host vs location.hostname 和跨浏览器兼容性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6725890/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com