JavaScript中的位置协议属性

JavaScript | 位置协议属性 (JavaScript | Location protocol property)

A protocol by definition simply implies a set or working rules that must be adhered to. A network protocol thus defines rules for communication between network devices. You must be familiar with a lot of protocols already such as the infamous http (hypertext transfer protocol), the ftp (file transfer protocol) for transferring files between a client and a server on a computer network smtp, https, etc. In JS, the protocol is a property attached to the location object.

协议从定义上仅意味着必须遵守的一套或工作规则。 因此,网络协议定义了网络设备之间通信的规则。 您必须已经熟悉许多协议,例如臭名昭​​著的http (超文本传输​​协议), ftp (文件传输协议),用于在计算机网络上的客户端和服务器之间传输文件smtphttps等。在JS中,协议是附加到位置对象的属性。

Let's open a new chrome tab and see this in action,

让我们打开一个新的chrome标签并查看实际效果,

console.log(location);
console.log(location.protocol);

Output

输出量

Location {href: "chrome-search://local-ntp/local-ntp.html", ancestorOrigins: DOMStringList, origin: "chrome-search://local-ntp", protocol: "chrome-search:", replace: ƒ, …}
https:

The protocol property returns the protocol of the current URL. According to MDN docs, it is a DOMString containing the host, that is the hostname, a ':', and the port of the URL. On the homepage of google chrome, we get a different protocol because they use a different custom protocol for their search engines. Okay, let's see a simpler example. Go to any normal website, for example, includehelp.com and inside the dev console type in,

protocol属性返回当前URL的协议。 根据MDN文档,它是一个包含主机的DOMString,即主机名,“:”和URL的端口。 在谷歌浏览器的首页上,我们获得了不同的协议,因为他们为搜索引擎使用了不同的自定义协议。 好吧,让我们看一个简单的例子。 转到任何常规网站(例如includehelp.com),然后在开发控制台中输入以下内容:

location.protocol;

Output

输出量

"https:"

The URL follows an https: protocol hence returns us a string containing the name of that protocol (in this case, https) along with a colon. If you simply type in the location object you will see various other properties associated with it. However, you can easily notice that the location object gets you the properties of the current location that your URL indicates. If you navigate to different pages you'll see different values of these properties following the URL or the website you're visiting.

URL遵循https:协议,因此返回一个字符串,其中包含该协议的名称(在本例中为https)以及冒号。 如果您仅键入位置对象,您将看到与之关联的各种其他属性。 但是,您可以轻松地注意到location对象为您提供了URL指示的当前位置的属性。 如果导航到其他页面,则会在您访问的URL或网站之后看到这些属性的不同值。

翻译自: https://www.includehelp.com/code-snippets/location-protocol-property-in-javascript.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值