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