สามารถใช้วัตถุ window.location เพื่อรับได้ ที่อยู่หน้าปัจจุบัน (URL) และเพื่อเปลี่ยนเส้นทางเบราว์เซอร์ไปยังหน้าใหม่
อ็อบเจ็กต์ window.location สามารถเขียนได้โดยไม่ต้องใช้คำนำหน้าหน้าต่าง
ตัวอย่างบางส่วน:
window.location.href ส่งคืน href (URL) ของหน้าปัจจุบัน
window.location.hostname ส่งคืนชื่อโดเมนของโฮสต์เว็บ
window.location.pathname ส่งคืนเส้นทางและชื่อไฟล์ของหน้าปัจจุบัน
window.location.protocol ส่งคืนโปรโตคอลเว็บที่ใช้ (http:หรือ https:)
window.location.assign() โหลดเอกสารใหม่
window.location.href คุณสมบัติส่งกลับ URL ของหน้าปัจจุบัน
แสดง href (URL) ของหน้าปัจจุบัน:
document.getElementById("demo").innerHTML =
"Page location is " + window.location.href;ผลลัพธ์คือ:
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<h3>The window.location object</h3>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"The full URL of this page is:<br>" + window.location.href;
</script>
</body>
</html>
คุณสมบัติ window.location.hostname ส่งคืนชื่อของโฮสต์อินเทอร์เน็ต (ของหน้าปัจจุบัน)
แสดงชื่อโฮสต์:
document.getElementById("demo").innerHTML =
"Page hostname is " + window.location.hostname;ผลลัพธ์คือ:
<script>
document.write("Page hostname is " + location.hostname);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<h3>The window.location object</h3>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"Page hostname is: " + window.location.hostname;
</script>
</body>
</html>
window.location.pathname คุณสมบัติส่งกลับชื่อเส้นทางของ หน้าปัจจุบัน
แสดงชื่อเส้นทางของ URL ปัจจุบัน:
document.getElementById("demo").innerHTML =
"Page path is " + window.location.pathname;ผลลัพธ์คือ:
<script>
document.write("Page path is " + location.pathname);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<h3>The window.location object</h3>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"Page path is: " + window.location.pathname;
</script>
</body>
</html>
คุณสมบัติ window.location.protocol ส่งคืนโปรโตคอลเว็บของเพจ
แสดงโปรโตคอลเว็บ:
document.getElementById("demo").innerHTML =
"Page protocol is " + window.location.protocol;ผลลัพธ์คือ:
<script>
document.write("Page protocol is " + location.protocol);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<h3>The window.location object</h3>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"The page protocol is: " + window.location.protocol;
</script>
</body>
</html>
คุณสมบัติ window.location.port ส่งคืนหมายเลขของโฮสต์อินเทอร์เน็ต พอร์ต (ของหน้าปัจจุบัน)
แสดงชื่อโฮสต์:
document.getElementById("demo").innerHTML =
"Port
number is " + window.location.port;ผลลัพธ์คือ:
<script>
document.write("Port number is " + location.port);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<h3>The window.location object</h3>
<p id="demo"></p>
<p><b>Note: </b>If the port number is default (80 for http and 443 for https), most browsers will display 0 or nothing.</p>
<script>
document.getElementById("demo").innerHTML =
"The URL port number of the current page is: " + window.location.port;
</script>
</body>
</html>
เบราว์เซอร์ส่วนใหญ่จะไม่แสดงหมายเลขพอร์ตเริ่มต้น (80 สำหรับ http และ 443 สำหรับ https)
window.location.assign() วิธีการโหลดเอกสารใหม่
โหลดเอกสารใหม่:
<html>
<head>
<script>
function newDoc() {
window.location.assign("https://www.w3schools.com")
}
</script>
</head>
<body>
<input type="button" value="Load new document"
onclick="newDoc()">
</body>
</html>ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<h3>The window.location object</h3>
<input type="button" value="Load new document" onclick="newDoc()">
<script>
function newDoc() {
window.location.assign("https://www.w3schools.com")
}
</script>
</body>
</html>