How can I redirect to another URL in a web page using JavaScript?
When you redirect a URL, you're simply forwarding it to another address on the same, or different domain. You can set up a redirect that sends visitors to your new domain name when they'll try to access a URL that belonged to your old domain.
window.location.href = "URL2"
inside a JS block on the page or in an included file; that's assuming you really want to do it on the client. Usually, the server sends the redirect via a 300-series response.
Since you tagged the question with javascript
and html
...
For a purely HTML solution, you can use a meta
tag in the header
to "refresh" the page, specifying a different URL:
<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.yourdomain.com/somepage.html">
If you can/want to use JavaScript, you can set the location.href
of the window
:
<script type="text/javascript"> window.location.href = "http://www.yourdomain.com/somepage.html"; </script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With