Javascript 如何重定向到主页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4231605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:29:46  来源:igfitidea点击:

how to redirect to home page

javascript

提问by Māris Kise?ovs

How can I redirect a user to home page?

如何将用户重定向到主页?

Example: mywebsite.com/ddfdf/fdfdsfand I want to redirect to mywebsite.com

示例:mywebsite.com/ddfdf/fdfdsf我想重定向到mywebsite.com

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

但是我想在不输入静态名称的情况下执行此操作。我怎样才能做到这一点?

回答by Māris Kise?ovs

document.location.href="/";

回答by A-Sharabiani

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.locationrather than document.location.

根据 W3C,它们是相同的。实际上,为了跨浏览器安全,您应该使用window.location而不是document.location.

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

请参阅:http: //www.w3.org/TR/Window/#window-location

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

注意:我从这个问题复制了上面的差异解释。)

回答by Jeff

window.location.href = "/";

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

这对我有用。如果你有多个文件夹/目录,你可以使用这个:

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

回答by Brandon Montgomery

maybe

也许

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

is what you're looking for?

是你要找的吗?

回答by PleaseStand

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

您可以在服务器上执行此操作,例如使用 Apache 的 mod_rewrite 吗?如果没有,您可以使用该window.location.replace方法从后退/前进历史记录中删除当前 URL(不破坏后退按钮)并转到网站的根目录:

window.location.replace('/');

回答by Ben Taliadoros

window.location = '/';

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

通常应该可以解决问题,但这取决于您的站点目录。这将适用于您的示例

回答by Shashikant Sejwar

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

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

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

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

Put this code in Page Load.

将此代码放在页面加载中。

回答by Akshay Mishra

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

请参阅此答案 https://stackoverflow.com/a/42291014/3901511