HTML DOM 位置埠屬性


Location port 屬性返回/設定 URL 的埠號(如果已指定)。如果沒有明確指定,可能不會顯示埠號。

語法

以下是語法 -

  • 返回 port 屬性的值
location.port
  • 設定 port 屬性的值
location.port = portNumber

示例

讓我們看看 Location port 屬性的示例 -

 動態演示

<!DOCTYPE html>
<html>
<head>
<title>Location port</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-port</legend>
<label for="urlSelect">Current URL:</label>
<input type="url" size="30" id="urlSelect" value="https://www.example.com:2544/aboutUs/CTO.htm">
<input type="button" onclick="getport()" value="Get Port">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var urlSelect = document.getElementById("urlSelect");
   function getport(){
      divDisplay.textContent = 'URL port: '+location.port;
   }
</script>
</body>
</html>

輸出

將產生以下輸出 -

在點選 '獲取埠' 按鈕之前 -

在點選 '獲取埠' 按鈕之後 -

更新於: 30-07-2019

60 次瀏覽

開啟您的職業生涯

完成課程即可獲得認證

開始學習
廣告