คุณสมบัติ HTML DOM Location href ส่งคืน/ตั้งค่าสตริงที่สอดคล้องกับเส้นทาง URL
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
- คืนค่าของ href ทรัพย์สิน
location.href
- ค่าของ href ชุดคุณสมบัติ
location.href = href
ตัวอย่าง
ให้เราดูตัวอย่างสำหรับ ตำแหน่ง href ทรัพย์สิน −
<!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> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
ก่อนคลิก ‘รับ href’ ปุ่ม −

หลังจากคลิก ‘รับ href’ ปุ่ม −
