คุณสมบัติแฮช Location ส่งกลับ/ผนวกค่าสตริง (ส่วนสมอ) เข้ากับ URL ส่วนสมอจะขึ้นต้นด้วย '#' โดยอัตโนมัติแล้วต่อท้าย
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
- คืนค่าของ แฮช ทรัพย์สิน
location.hash
- คุณค่าของ แฮช ชุดคุณสมบัติ
location.hash = ‘string’
ตัวอย่าง
ให้เราดูตัวอย่างสำหรับ แฮชตำแหน่ง ทรัพย์สิน −
<!DOCTYPE html>
<html>
<head>
<title>Location hash</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-hash</legend>
<label for="urlSelect">Current URL:</label>
<input type="url" size="27" id="urlSelect" value="https://www.example.com/">
<input type="text" id="textSelect" placeholder="hash value...">
<input type="button" onclick="setHashValue()" value="Go To URL">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var urlSelect = document.getElementById("urlSelect");
var textSelect = document.getElementById("textSelect");
function setHashValue() {
if(textSelect.value !== ''){
location.hash = textSelect.value;
urlSelect.value += location.hash;
divDisplay.textContent = 'Page loaded. Current URL active';
} else {
divDisplay.textContent = 'Please provide a hash value';
}
}
</script>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
ก่อนคลิก 'ไปที่ URL' ปุ่ม −

หลังจากคลิก 'ไปที่ URL' ปุ่มพร้อมชุดค่าแฮช -
