Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> HTML

Thuộc tính protocol của Location trong HTML DOM

Thuộc tính protocol của Location trong HTML DOM trả về/thiết lập 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 là 'file:', 'http:', 'https:', v.v.

Cú pháp

Trả về giá trị thuộc tính protocol:

location.protocol

Thiết lập giá trị thuộc tính protocol:

location.protocol = protocolString

Ví dụ

Ví dụ minh họa thuộc tính 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">URL hiện tại:</label> <input type="url" size="30" id="urlSelect" value="https://www.example.com:2544/aboutUs/CTO.htm"> <input type="button" onclick="getprotocol()" value="Lấy giao thức"> <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>

Kết quả

Trước khi nhấn nút 'Lấy giao thức':

Thuộc tính protocol của Location trong HTML DOM

Sau khi nhấn nút 'Lấy giao thức':

Thuộc tính protocol của Location trong HTML DOM