JavaScript can navigate the current page to another URL through location. For a normal navigation that should remain in Back history, use location.assign() or assign to location.href.
location.assign('https://www.example.com/');
The original tutorial used window.location = "..."; that form still navigates, but an explicit Location method often communicates intent more clearly.
window.location = 'https://www.example.com/';
The original self.location form is also retained as an equivalent reference inside a normal window:
self.location = 'https://www.example.com/';
If client-side JavaScript is disabled, a JavaScript-only redirect will not run. You can check the browser JavaScript-support example, but server-side HTTP redirects are usually the better choice when a redirect is required for routing, canonicalization, moved content, or SEO.
If a delayed transition is part of the interface, use a function callback with setTimeout(), not a string containing JavaScript. The original tutorial connected this topic to the JavaScript timer tutorial.
setTimeout(() => {
location.assign('https://www.example.com/');
}, 10000);
The original example used a 10-second delay. If you deliberately delay a user-visible navigation, show the destination and remaining time, and provide a direct link so users are not trapped waiting.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript page redirect</title>
</head>
<body>
<p>You will be redirected after 10 seconds.
<a href="https://www.example.com/">Open the destination now</a>.
</p>
<script>
setTimeout(() => {
location.assign('https://www.example.com/');
}, 10000);
</script>
</body>
</html>
Use location.replace() when the current page should not remain as a Back-history entry.
location.replace('https://www.example.com/');
Redirecting page based on Selection of Dropdown menu
Window Object Bookmark a page Displaying confirmation window
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| sdaf | 05-07-2010 |
| Nice tutorial Great. | |