how to redirect to home page

Javascript

Javascript Problem Overview


How can I redirect a user to home page?

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

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

Javascript Solutions


Solution 1 - Javascript

document.location.href="/";

Solution 2 - Javascript

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.)

Solution 3 - Javascript

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('/');

Solution 4 - Javascript

window.location.href = "/";

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

window.location.href = "/folder_name/";

Solution 5 - Javascript

maybe

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

is what you're looking for?

Solution 6 - Javascript

window.location = '/';

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

Solution 7 - Javascript

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

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

Put this code in Page Load.

Solution 8 - Javascript

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

See this answer https://stackoverflow.com/a/42291014/3901511

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteven SpielbergView Question on Stackoverflow
Solution 1 - JavascriptMāris KiseļovsView Answer on Stackoverflow
Solution 2 - JavascriptA-SharabianiView Answer on Stackoverflow
Solution 3 - JavascriptPleaseStandView Answer on Stackoverflow
Solution 4 - JavascriptJeffView Answer on Stackoverflow
Solution 5 - JavascriptBrandon MontgomeryView Answer on Stackoverflow
Solution 6 - JavascriptBen TaliadorosView Answer on Stackoverflow
Solution 7 - JavascriptShashikant SejwarView Answer on Stackoverflow
Solution 8 - JavascriptAkshay MishraView Answer on Stackoverflow