วิธีการแทนที่ตำแหน่ง HTML DOM () ใช้สำหรับแสดงเอกสารใหม่แทนที่เอกสารปัจจุบัน นอกจากนี้ยังลบ URL เอกสารปัจจุบันออกจากประวัติเอกสารที่ปิดใช้งานการนำทางไปยังเอกสารเก่าโดยใช้ 'ย้อนกลับ' ปุ่ม.
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
location.replace(URLString)
ตัวอย่าง
ให้เราดูตัวอย่างสำหรับ Location replace() ทรัพย์สิน −
<!DOCTYPE html>
<html>
<head>
<title>Location replace()</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-replace( )</legend>
<label for="urlSelect">Current URL: </label>
<input type="url" id="urlSelect" value="https://www.example.com"><br>
<label for="newUrlSelect">New URL: </label>
<input type="url" id="newUrlSelect" placeholder="Give new url..."><br>
<input type="button" onclick="doReplace()" value="Go to new URL">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var urlSelect = document.getElementById("urlSelect");
var newUrlSelect = document.getElementById("newUrlSelect");
function doReplace(){
if(newUrlSelect.value === '')
divDisplay.textContent = 'Provide new URL';
else{
location.replace(newUrlSelect.value);
divDisplay.textContent = 'Redirecting to new URL';
}
}
</script>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
หลังจากคลิก 'ไปที่ URL ใหม่' ปุ่มที่ไม่มีอินพุต -

คลิก 'ไปที่ URL ใหม่' ปุ่มหลังจากกรอกรายละเอียด -
