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 (文件傳輸協議),用于在計算機網絡上的客戶端和服務器之間傳輸文件smtp , https等。在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