Thuộc tính Giao thức vị trí trả về / đặt một chuỗi tương ứng với giao thức được sử dụng cho một URL. Giao thức có thể được đặt thành ‘tệp:’, ‘http:’, ‘https:’, v.v ..
Cú pháp
Sau đây là cú pháp -
- Giá trị trả về của giao thức tài sản
location.protocol
- Giá trị của giao thức bộ tài sản
location.protocol = protocolString
Ví dụ
Hãy để chúng tôi xem một ví dụ về Giao thức vị trí tài sản -
<!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> Đầu ra
Điều này sẽ tạo ra kết quả sau -
Trước khi nhấp vào ‘Nhận giao thức’ nút -
Sau khi nhấp vào ‘Nhận giao thức’ nút -