Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

คุณสมบัติ protocol ของ Location ใน HTML DOM

คุณสมบัติ protocol ของ Location ใช้สำหรับอ่านหรือตั้งค่าพรอทอคอลของ URL (เช่น 'http:', 'https:', 'file:')

ไวยากรณ์ (Syntax)

การอ่านค่า

location.protocol

การตั้งค่า

location.protocol = protocolString

ตัวอย่างการใช้งาน

ตัวอย่างด้านล่างแสดงพรอทอคอลของ URL ปัจจุบัน:

<!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>

ผลลัพธ์

ก่อนคลิกปุ่ม "Get protocol": แสดง URL และปุ่ม

หลังคลิกปุ่ม "Get protocol": แสดง "URL protocol: https:" (ขึ้นอยู่กับหน้าปัจจุบัน)

หมายเหตุ: รูปภาพตัวอย่างจากต้นฉบับไม่ได้รวมอยู่ในเนื้อหานี้