Thuộc tính pathname của đối tượng Location trả về hoặc thiết lập chuỗi tương ứng với đường dẫn (pathname) của URL.
Cú pháp
Lấy giá trị thuộc tính pathname
location.pathname
Thiết lập giá trị thuộc tính pathname
location.pathname = pathOfHost
Ví dụ
Dưới đây là ví dụ minh họa cho thuộc tính Location pathname:
<!DOCTYPE html> <html> <head> <title>Location pathname</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-pathname</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="getpathname()" value="Lấy pathname"> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); var urlSelect = document.getElementById("urlSelect"); function getpathname(){ divDisplay.textContent = 'URL pathname: '+location.pathname; } </script> </body> </html>
Kết quả
Ví dụ này sẽ cho kết quả sau:
Trước khi nhấn nút 'Lấy pathname':

Sau khi nhấn nút 'Lấy pathname':
