HTML DOM Location host 屬性
Location host 屬性返回/設定主機名和主機埠(如果已指定)。如果未明確指定,埠可能不會顯示。
語法
語法如下 −
- 返回值 host 屬性
location.host
- 值host 屬性設定
location.host = hostname:host
示例
我們來看看 Location host 屬性的一個示例 −
<!DOCTYPE html>
<html>
<head>
<title>Location host</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
}
input[type="button"] {
border-radius: 10px;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>Location-host</legend>
<label for="urlSelect">Current URL:</label>
<input type="url" size="27" id="urlSelect" value="https://www.example.com:2544">
<input type="button" onclick="getHostPort()" value="Get Port">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var urlSelect = document.getElementById("urlSelect");
function getHostPort() {
if(location.host.includes(':') === false)
divDisplay.textContent = 'No port present, as host: '+location.host;
else
divDisplay.textContent = 'Port: '+location.host.split(":")[1]+', as host: '+location.host;
}
</script>
</body>
</html>輸出
這會產生以下輸出 −
點選‘獲取埠’按鈕之前 −

點選‘獲取埠’按鈕之後 −

廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP