Skip to main content
PHP Blog

Back to all posts

How to Redirect to Another Page In JavaScript?

Published on
3 min read
How to Redirect to Another Page In JavaScript? image

Best JavaScript Tutorials to Buy in October 2025

1 JavaScript: The Definitive Guide: Master the World's Most-Used Programming Language

JavaScript: The Definitive Guide: Master the World's Most-Used Programming Language

BUY & SAVE
$34.30 $79.99
Save 57%
JavaScript: The Definitive Guide: Master the World's Most-Used Programming Language
2 JAVASCRIPT Programming, For Beginners, Quick Start Guide!: JavaScript Language Crash Course Tutorial (Paperbacks in 8 Hours)

JAVASCRIPT Programming, For Beginners, Quick Start Guide!: JavaScript Language Crash Course Tutorial (Paperbacks in 8 Hours)

BUY & SAVE
$13.99
JAVASCRIPT Programming, For Beginners, Quick Start Guide!: JavaScript Language Crash Course Tutorial (Paperbacks in 8 Hours)
3 Learn Enough JavaScript to Be Dangerous: A Tutorial Introduction to Programming with JavaScript

Learn Enough JavaScript to Be Dangerous: A Tutorial Introduction to Programming with JavaScript

  • DEPLOY JAVASCRIPT APPS QUICKLY FOR IMMEDIATE WEB PRESENCE.
  • HARNESS FUNCTIONAL PROGRAMMING FOR CLEANER, EFFICIENT CODE.
  • BUILD INTERACTIVE, ROBUST WEBSITES WITH MODULAR NPM PACKAGES.
BUY & SAVE
$39.99
Learn Enough JavaScript to Be Dangerous: A Tutorial Introduction to Programming with JavaScript
4 Eloquent JavaScript, 3rd Edition: A Modern Introduction to Programming

Eloquent JavaScript, 3rd Edition: A Modern Introduction to Programming

BUY & SAVE
$17.99 $39.99
Save 55%
Eloquent JavaScript, 3rd Edition: A Modern Introduction to Programming
5 Beginning JavaScript

Beginning JavaScript

BUY & SAVE
$23.63 $47.00
Save 50%
Beginning JavaScript
6 Advanced Perl Programming: The Worlds Most Highly Developed Perl Tutorial

Advanced Perl Programming: The Worlds Most Highly Developed Perl Tutorial

  • AFFORDABLE PRICES ON QUALITY USED BOOKS FOR SAVVY READERS.
  • THOROUGHLY INSPECTED FOR QUALITY TO ENSURE A SATISFYING READ.
  • ECO-FRIENDLY CHOICE: PROMOTE SUSTAINABILITY WITH EVERY PURCHASE.
BUY & SAVE
$15.62 $44.99
Save 65%
Advanced Perl Programming: The Worlds Most Highly Developed Perl Tutorial
+
ONE MORE?

In JavaScript, you can use the window.location property to redirect to another page by assigning a new URL to it. Here is the syntax you can use:

window.location.href = "newPage.html";

This line of code will redirect the user to the specified URL, in this case, "newPage.html". You can replace "newPage.html" with any other valid URL.

You can also use the window.location.replace() method to achieve the same result. Here is an example:

window.location.replace("newPage.html");

Both methods perform a redirect, but the difference is that the replace() method removes the current URL from the browser history, while the href property adds a new entry to the history.

It's important to note that JavaScript page redirects should generally be used sparingly and with caution, as excessive or unnecessary redirects can cause a poor user experience.

How do you redirect to a different page after a form submission?

To redirect to a different page after a form submission, you can use one of the following methods:

  1. Using the HTML form tag: In this method, the action attribute of the form tag specifies the URL of the page where the form data should be submitted. Once the form is submitted, the browser will automatically navigate to the specified page.
  2. Using JavaScript: In this method, the onsubmit event of the form tag is intercepted using a JavaScript function. The event.preventDefault() method prevents the default form submission behavior. After handling the form submission, you can use window.location.href to redirect the user to the desired page.
  3. Using server-side programming: If you are using server-side programming, such as PHP, you can handle the form submission on the server and then use server-side code to redirect to a different page. For example, in PHP: After processing the form data, you can use header("Location: your_target_page.php") to redirect the user to the desired page. The exit function ensures that the script execution ends immediately after the redirect.

Choose the method that suits your needs and implement it based on your programming environment.

What is JavaScript?

JavaScript is a high-level programming language that is primarily used to create interactive web pages and web applications. It is often abbreviated as JS. It allows developers to add dynamic and interactive elements to websites, such as user input forms, animations, and content updates. JavaScript can be used both on the client-side, meaning it runs on the user's web browser, and on the server-side, meaning it runs on the web server. It is one of the core technologies for web development along with HTML and CSS.

What is setTimeout() used for in JavaScript?

setTimeout() is used in JavaScript to delay the execution of a code or function for a specified amount of time. It takes two parameters: a callback function or a code to be executed, and a delay time in milliseconds. The code or function specified will be executed once after the specified delay time has passed. It is commonly used to create a delay before performing certain actions, implementing animations, or scheduling tasks that require a delay.