本文翻译自: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 +中确定其他所有其他内容?


#1楼

参考:https://stackoom.com/question/SDi6/location-host与location-hostname和跨浏览器的兼容性


#2楼

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

#3楼

Your primary question has been answered above. 您的主要问题已在上面得到解答。 I just wanted to point out that the regex you're using has a bug. 我只是想指出你正在使用的正则表达式有一个错误。 It will also succeed on foo-domain.com which is not a subdomain of domain.com 它也将在foo-domain.com上成功,而foo-domain.com不是domain.com的子domain.com

What you really want is this: 你真正想要的是这个:

/(^|\.)domain\.com$/

#4楼

If you are insisting to use the window.location.origin You can put this in top of your code before reading the origin 如果您坚持使用window.location.origin您可以在阅读origin之前将其置于代码之上

if (!window.location.origin) {window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
}

Solution

PS: For the record, it was actually the original question. PS:为了记录,它实际上是原始问题。 It was already edited :) 它已经编辑:)


#5楼

Just to add a note that Google Chrome browser has origin attribute for the location. 只是添加一条说明Google Chrome浏览器具有该位置的原始属性。 which gives you the entire domain from protocol to the port number as shown in the below screenshot. 它为您提供从协议到端口号的整个域,如下面的屏幕截图所示。


#6楼

host just includes the port number if there is one specified. 如果有指定的端口号,则主机只包含端口号。 If there is no port number specifically in the URL, then it returns the same as hostname. 如果URL中没有专门的端口号,则返回与hostname相同的端口号。 You pick whether you care to match the port number or not. 您选择是否要匹配端口号。 See https://developer.mozilla.org/en/window.location for more info. 有关详细信息,请参阅https://developer.mozilla.org/en/window.location 。

I would assume you want hostname to just get the site name. 我想你想要主机名来获取网站名称。

location.host与location.hostname和跨浏览器的兼容性?相关推荐

  1. location.host 与 location.hostname 的区别

    作者:vkvi 来源:千一网络(原创) 时间:2011-1-31 试过 jQuery,再试试 ezj. JavaScript 中,大多数情况下,我们不会发现 location.host 与 locat ...

  2. Selenium Grid跨浏览器-兼容性测试

    Selenium Grid跨浏览器-兼容性测试 这里有两台机子,打算这样演示: 一台机子启动一个作为主点节的hub 和 一个作为次节点的hub(系统windows 浏览器为ie) ip为:192.16 ...

  3. 系统上线发布清单_跨浏览器测试清单上线之前

    系统上线发布清单 This article was originally published on LambdaTest. Thank you for supporting the partners ...

  4. 解决安卓微信浏览器中location.reload 或者 location.href失效的问题

    解决安卓微信浏览器中location.reload 或者 location.href失效的问题 参考文章: (1)解决安卓微信浏览器中location.reload 或者 location.href失 ...

  5. 判断域名来源的操作【window.location.host.indexOf(‘域名关键词‘)】 - 代码篇

    文章目录 判断域名来源的操作 - 代码篇 代码如下: 判断域名来源的操作 - 代码篇 代码如下: // 判断域名的操作:如果域名是qblog 或qdownload,则为true: var flagBo ...

  6. 在JS中用window location host获取域名

    做了一个通用的用户反馈系统,所有平台都可以接入,但是要对应不同的平台存入不同的用户反馈,所以有个platform字段存放平台名称.但是每个平台的platform名称又是不一样的,以通过域名来标识具体是 ...

  7. window.location和document.location的区别分析

    用户不能改变document.location(因为这是当前显示文档的位置).但是,可以改变window.location (用其它文档取代当前文档)window.location本身也是一个对象,而 ...

  8. document.location和window.location有什么区别就是

    用户不能改变document.location(因为这是当前显示文档的位置).但是,可以改变window.location (用其它文档取代当前文档),window.location本身也是一个对象, ...

  9. location.reload()和location.replace()的区别与应用

    location.reload()和location.replace()的区别与应用 语法 首先介绍这两个方法的语法 1.reload(): 强迫浏览器刷新当前页面 语法: location.relo ...

最新文章

  1. SnackBar使用注意事项
  2. netstat -ano 查看机器端口占用情况
  3. Android自定义View实践 空气质量检测 pm2.5
  4. 【学术相关】11 个好用的科研工具推荐!工作效率提升 max!
  5. JIRA中vm后缀文件语法说明
  6. 如何更新 Linux 内核来提升系统性能
  7. oracle导出导入数据库
  8. 获取Linux命令源代码的方法【ZT】
  9. leaflet.toolbar.js
  10. 蛇形填数 ------- 模拟水题
  11. Android Studio升级到3.0.0后构建项目时出现的问题总结
  12. java连接数据库配置文件
  13. 2016手机数据恢复软件哪个好?
  14. 使用HTML5仿制的QQ音乐PC版界面
  15. qq登录界面句柄_天天玩QQ!知道登录界面那两个人是谁吗?网友:不是情侣?...
  16. 优质的APP推广渠道那么多,怎么选才好?
  17. GBase 8s 入门
  18. 智慧农业智能节水灌溉 机井灌溉控制器
  19. sql 根据出生日期计算年龄
  20. ADAS/AD控制器模块开发11 - UDS与配置、校准

热门文章

  1. Tomcat运行时报内存溢出
  2. 【SSO单点系列】(6):CAS4.0 单点流程序列图(中文版)以及相关术语解释(TGT、ST、PGT、PT、PGTIOU)...
  3. [数据结构复习]B树B+树
  4. Linux命令:文件和目录操作
  5. jPList – 实现灵活排序和分页功能的 jQuery 插件
  6. Ceylon 1.0.0
  7. 2008年12月信息处理技术员上午试卷 51CTO版参考答案
  8. JavaScript:年月日時分秒設置
  9. JavaScript(3)——Object-Oriented Design
  10. hdu1824-Let's go home:图论2-SAT