By bitmap
Hello,
What is the best way of redirecting a user to another website or page using pure JavaScript?
Thank you!
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Accepted Answer
Hey there,
One way to do that would be to use the location.href. Here’s an example:
window.location.href = "https://digitalocean.com";
Here is a quick demo:

If you want to redirect the user to a specific page on your site just specify the page itself:
window.location.href = "some-page.html";
Another option would be to use location.replace instead:
window.location.replace("https://digitalocean.com");
One of the main differences between location.replace and location.href is that with location.replace the user’s current page will not be saved in the session, which means that your users will not be able to go back to the old page with the ‘Back’ button.
If you are new to JavaScript I would highly recommend this tutorial series here:
https://www.digitalocean.com/community/tutorial_series/how-to-code-in-javascript
Hope that this helps! Regards, Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.