เพื่อเปลี่ยนเส้นทางไปยังหน้าเว็บอื่นด้วย 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>ผลลัพธ์
โค้ดข้างต้นจะสร้างผลลัพธ์ดังนี้ -

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