Thuộc tính port của Location trong HTML DOM trả về/thiết lập số cổng (nếu được chỉ định) cho một URL. Số cổng có thể không hiển thị nếu không được chỉ định rõ ràng.
Cú pháp
Trả về giá trị thuộc tính port:
location.port
Thiết lập giá trị thuộc tính port:
location.port = portNumber
Ví dụ
Ví dụ minh họa thuộc tính 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">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="getport()" value="Lấy cổng"> <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>
Kết quả
Trước khi nhấn nút 'Lấy cổng':

Sau khi nhấn nút 'Lấy cổng':
