如何在 JavaScript 中重定向到主页?
如何将用户重定向到主页?
示例: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以使用下面的代码重定向到主页。 *位置,location.href 和 location.assign() 重定向到将记录添加到历史记录中的 URL,以便我们可以返回到上一页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:
或者
根据 W3C,它们是相同的。实际上,为了跨浏览器安全,您应该使用
window.location
而不是document.location
。请参阅:http://www.w3.org/TR/Window/#window-location
(注意:我从这个 问题。)
or
According to the W3C, they are the same. In reality, for cross browser safety, you should use
window.location
rather thandocument.location
.See: http://www.w3.org/TR/Window/#window-location
(Note: I copied the difference explanation above, from this question.)
这对我有用。
如果您有多个文件夹/目录,可以使用以下命令:
This worked for me.
If you have multiple folders/directories, you can use this:
您可以在服务器上执行此操作,例如使用 Apache 的 mod_rewrite 吗?如果没有,您可以使用
window.location.replace
方法< /a> 从后退/前进历史记录中删除当前 URL(以免破坏后退按钮)并转到网站的根目录: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:也许
就是您正在寻找的?
maybe
is what you're looking for?
通常应该可以解决问题,但这取决于您的网站目录。这适用于你的例子
Should usually do the trick, but it depends on your sites directories. This will work for your example
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.
看这个答案
https://stackoverflow.com/a/42291014/3901511
See this answer
https://stackoverflow.com/a/42291014/3901511