Thuộc tính HTML DOM Location href trả về / đặt chuỗi tương ứng với đường dẫn URL.
Cú pháp
Sau đây là cú pháp -
- Giá trị trả về của href tài sản
location.href
- Giá trị của href bộ tài sản
location.href = href
Ví dụ
Hãy để chúng tôi xem một ví dụ về Location href tài sản -
<!DOCTYPE html>
<html>
<head>
<title>Location href</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-href</legend>
<label for="urlSelect">Current URL:</label>
<input type="url" size="30" id="urlSelect" value="https://www.example.com/aboutUs">
<input type="button" onclick="gethref()" value="Get href">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var urlSelect = document.getElementById("urlSelect");
function gethref(){
divDisplay.textContent = 'URL Path: '+location.href;
}
</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 href’ nút -
Sau khi nhấp vào ‘Nhận href’ nút -