如何在 JavaScript 中重定向到主页?

发布于 2024-10-03 10:48:43 字数 147 浏览 4 评论 0原文

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

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

但是我想在不输入静态名称的情况下执行此操作。我该怎么做?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

梨涡少年 2024-10-10 10:48:44

您可以使用下面的代码重定向到主页。 *位置location.hreflocation.assign() 重定向到将记录添加到历史记录中的 URL,以便我们可以返回到上一页location.replace() 重定向到不将记录添加到历史记录中,这样我们就无法返回到上一页:

location="/";
location.href="/";
location.assign("/");
location.replace("/");

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("/");
小耗子 2024-10-10 10:48:43
document.location.href="/";
document.location.href="/";
罪#恶を代价 2024-10-10 10:48:43
document.location.href="/";

或者

 window.location.href = "/";

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

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

注意:我从这个 问题。)

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

巷子口的你 2024-10-10 10:48:43
window.location.href = "/";

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

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

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

window.location.href = "/folder_name/";
白日梦 2024-10-10 10:48:43

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

window.location.replace('/');

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('/');
∞觅青森が 2024-10-10 10:48:43

也许

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

就是您正在寻找的?

maybe

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

is what you're looking for?

我很坚强 2024-10-10 10:48:43
window.location = '/';

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

window.location = '/';

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

养猫人 2024-10-10 10:48:43

strRetMsg ="";

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

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

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

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

Put this code in Page Load.

淡看悲欢离合 2024-10-10 10:48:43
var url = location.href;
var newurl = url.replace('some-domain.example','another-domain.example';);
location.href=newurl;

看这个答案
https://stackoverflow.com/a/42291014/3901511

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

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

被翻牌 2024-10-10 10:48:43
var url = location.href;
var newurl = url.replace('some-domain.example','another-domain.example';);
location.href=newurl;

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文