HTML DOM Location protocol 屬性


Location protocol 屬性返回/設定與 URL 對應的協議字串。協議可以設為“檔案:”,“http:”,“https:”等。

語法

語法如下 −

  • 返回值 protocol 屬性
location.protocol
  • Value of the protocol property set
location.protocol = protocolString

示例

讓我們看一個設定 Location protocol 屬性的示例 −

 線上演示

<!DOCTYPE html>
<html>
<head>
<title>Location protocol</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-protocol</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="getprotocol()" value="Get protocol">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var urlSelect = document.getElementById("urlSelect");
   function getprotocol(){
      divDisplay.textContent = 'URL protocol: '+location.protocol;
   }
</script>
</body>
</html>

輸出

輸出內容如下 −

單擊“獲取協議”按鈕之前 −

單擊“獲取協議”按鈕之後 −

更新日期:2019 年 7 月 30 日

67 次瀏覽

啟動您的 職業生涯

完成課程即可獲得認證

開始學習
廣告
© . All rights reserved.