How to redirect to home page in JavaScript?
Asked Answered
T

10

93

How can I redirect a user to home page?

Example: mywebsite.example/ddfdf/fdfdsf and I want to redirect to mywebsite.example

However I want to do it without typing the static name. How can I do this?

Trengganu answered 20/11, 2010 at 7:4 Comment(1)
Question could be better worded to include "with Javascript" or "using Javascript" if that was what you were looking for.Oligopsony
W
184
document.location.href="/";
Windom answered 20/11, 2010 at 7:8 Comment(1)
<span href='document.location.href="/";'>Go to homepage</span> or put it into a function and call it when he clicks on somethingNedra
S
41
document.location.href="/";

or

 window.location.href = "/";

According to the W3C, they are the same. In reality, for cross browser safety, you should use window.location rather than document.location.

See: http://www.w3.org/TR/Window/#window-location

(Note: I copied the difference explanation above, from this question.)

Solis answered 27/10, 2015 at 22:39 Comment(0)
D
10
window.location.href = "/";

This worked for me. If you have multiple folders/directories, you can use this:

window.location.href = "/folder_name/";
Devland answered 13/6, 2017 at 10:12 Comment(0)
A
6

Can you do this on the server, using Apache's mod_rewrite for example? If not, you can use the window.location.replace method to erase the current URL from the back/forward history (to not break the back button) and go to the root of the web site:

window.location.replace('/');
Ashmore answered 20/11, 2010 at 7:9 Comment(0)
K
3

maybe

var re = /^https?:\/\/[^/]+/i;
window.location.href = re.exec(window.location.href)[0];

is what you're looking for?

Kast answered 20/11, 2010 at 7:9 Comment(1)
This will break the back button if it is done automatically on page load.Ashmore
S
2
window.location = '/';

Should usually do the trick, but it depends on your sites directories. This will work for your example

Smashandgrab answered 13/1, 2014 at 12:32 Comment(0)
N
0

strRetMsg ="<script>window.location.href = '../Other/Home.htm';</script>";

Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", strRetMsg,false);

Put this code in Page Load.

Nosography answered 14/8, 2015 at 6:27 Comment(0)
V
0

var url = location.href;
var newurl = url.replace('some-domain.example','another-domain.example';);
location.href=newurl;

See this answer https://mcmap.net/q/225598/-301-redirect-blogger-to-another-host-and-keep-google-rank

Victorvictoria answered 17/2, 2017 at 6:43 Comment(0)
H
0

var url = location.href;
var newurl = url.replace('some-domain.example','another-domain.example';);
location.href=newurl;
Hally answered 21/7, 2022 at 23:21 Comment(0)
I
0

You can redirect to home page with the code below. *location, location.href and location.assign() redirect to the URL adding the record to the history so we can go back to the previous page while location.replace() redirects to the URL not adding the record to the history so we cannot go back to the previous page:

location="/";
location.href="/";
location.assign("/");
location.replace("/");
Isoline answered 2/8, 2023 at 7:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.