Answered by
Oliver Hall
A 302 redirect is a temporary redirection from one URL to another. In JavaScript, this can be achieved by modifying the window.location
property. When you set window.location
, the browser interprets it as if the user is being directed to a new URL.
Here’s how you can implement a 302 redirect using JavaScript:
// JavaScript to perform a 302 redirect function redirect(url) { window.location = url; } // Usage redirect('https://www.example.com');
This function redirect
takes a URL as an argument and assigns it to window.location
, causing the browser to navigate to the specified URL. This method is akin to a 302 redirect because it's generally understood that redirects done via JavaScript are temporary, much like the HTTP status code 302 suggests.
Using JavaScript for a redirect is simple and effective for client-side operations, but consider the context and specific needs of your project before deciding on this approach.