หากต้องการเปลี่ยนเส้นทางไปยังหน้าเว็บอื่นโดยใช้ JavaScript รหัสจะเป็นดังนี้ -
ตัวอย่าง
<!DOCTYPE html>
<html>
<head>
<h1>Redirect to a Webpage Example</h1>
<button class="redirectBtn">Redirect</button>
<h2>Click the above button to Redirect to another Webpage</h2>
<script>
document .querySelector(".redirectBtn") .addEventListener("click", redirectFunction);
function redirectFunction() {
location.href("https://tutorialspoint.com/");
}
</script>
</body>
</html> ผลลัพธ์
รหัสข้างต้นจะสร้างผลลัพธ์ต่อไปนี้ -

เมื่อคลิกปุ่ม "เปลี่ยนเส้นทาง" เราจะเปลี่ยนเส้นทางไปยังไซต์ใหม่ -
