วัตถุหน้าต่างรวมถึงวัตถุตำแหน่งใน JavaScript ประกอบด้วยคุณสมบัติดังต่อไปนี้ −
window.location.href
ส่งคืน URL ของหน้าปัจจุบัน
ตัวอย่าง
<!DOCTYPE html>
<html>
<body>
<p>Click below to get the complete URL of the page.</p>
<button onclick = "display()">URL</button>
<script>
function display() {
var res = location.href;
document.write(res);
}
</script>
</body>
</html> window.location.replace
ใช้เพื่อแทนที่เอกสารปัจจุบัน
ตัวอย่าง
<!DOCTYPE html>
<html>
<body>
<button onclick = "display()">Replace current document</button>
<script>
function display() {
location.replace("https://www.qries.com")
}
</script>
</body>
</html> window.location.assign
หากคุณต้องการโหลดเอกสารใหม่ ให้ใช้ JavaScript assign
ตัวอย่าง
<!DOCTYPE html>
<html>
<body>
<button onclick = "display()">Open new document</button>
<script>
function display() {
location.assign("https://www.qries.com")
}
</script>
</body>
</html>