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 September 2026

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
$38.99 $79.99
Save 51%
JavaScript: The Definitive Guide: Master the World's Most-Used Programming Language
2 Javascript Flashcards – 130-Cards | Learn Javascript Concepts & Syntax | 11 Sections for Beginners & Advanced Coders

Javascript Flashcards – 130-Cards | Learn Javascript Concepts & Syntax | 11 Sections for Beginners & Advanced Coders

  • MASTER JAVASCRIPT FAST WITH 130 CURATED FLASHCARDS FOR ALL LEVELS.
  • LEARN THROUGH REAL-WORLD EXAMPLES TO REINFORCE CORE CONCEPTS EFFECTIVELY.
  • IDEAL FOR STUDENTS AND SELF-TAUGHT PROGRAMMERS SEEKING QUICK REVISION.
BUY & SAVE
$27.99
Javascript Flashcards – 130-Cards | Learn Javascript Concepts & Syntax | 11 Sections for Beginners & Advanced Coders
3 Eloquent JavaScript, 3rd Edition: A Modern Introduction to Programming

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

BUY & SAVE
$13.65 $39.99
Save 66%
Eloquent JavaScript, 3rd Edition: A Modern Introduction to Programming
4 JavaScript: JavaScript Coding. From Zero to Hero in 8 Hours. JavaScript Programming: Learn Programming in Easy Way. An Ultimate Beginner's Guide! (Cookbooks in 8 Hours Book 20)

JavaScript: JavaScript Coding. From Zero to Hero in 8 Hours. JavaScript Programming: Learn Programming in Easy Way. An Ultimate Beginner's Guide! (Cookbooks in 8 Hours Book 20)

BUY & SAVE
$0.99
JavaScript: JavaScript Coding. From Zero to Hero in 8 Hours. JavaScript Programming: Learn Programming in Easy Way. An Ultimate Beginner's Guide! (Cookbooks in 8 Hours Book 20)
5 A Smarter Way to Learn JavaScript. The new tech-assisted approach that requires half the effort

A Smarter Way to Learn JavaScript. The new tech-assisted approach that requires half the effort

BUY & SAVE
$12.72 $19.95
Save 36%
A Smarter Way to Learn JavaScript. The new tech-assisted approach that requires half the effort
6 Javascript & Data Structures Study Flashcards – Master Core Concepts, Algorithms, and Interview-Ready Developer Skills

Javascript & Data Structures Study Flashcards – Master Core Concepts, Algorithms, and Interview-Ready Developer Skills

  • MASTER JAVASCRIPT WITH REAL-WORLD EXAMPLES FOR ALL SKILL LEVELS.
  • INTERACTIVE EXERCISES BOOST YOUR SKILLS AND REINFORCE LEARNING.
  • LEARN ANYWHERE, ANYTIME WITH OUR PORTABLE AND FLEXIBLE RESOURCES.
BUY & SAVE
$24.99
Javascript & Data Structures Study Flashcards – Master Core Concepts, Algorithms, and Interview-Ready Developer Skills
+
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.