【问题标题】:Redirection using javascript使用 JavaScript 重定向
【发布时间】:2016-10-12 09:26:54
【问题描述】:

我有一个 ASP.NET MVC 5 应用程序,我想在其中使用 Javascript 执行重定向:

var sPageURL = decodeURIComponent(window.location);
console.log(sPageURL);
var lengthUrl = sPageURL.split('/').length;
var NewUrl = '';
sPageURL.split('/').forEach(function(item, index) {
  if (index < lengthUrl - 2) NewUrl += item;
  if (index == lengthUrl - 1) {
    if (item == "Organisateur") NewUrl += sPageURL.split('/')[lengthUrl - 2];
  }
});
console.log(NewUrl);
window.location = NewUrl;

我得到输出:

http://localhost:31569/Event/2

http://localhost:31569/Event/localhost:31569

问题是新 URL 与旧 URL 连接在一起:我想将 http://localhost:31569/Event/2 替换为 localhost:31569

我尝试了window.location =window.location.href =window.location.replace,得到了相同的结果。

所以我需要知道:

  1. 这个问题的原因是什么?
  2. 我该如何解决?

【问题讨论】:

  • 如果您想导航到 URL 的根目录,为什么不直接使用 window.location = "/"
  • @KianCross 很好用:一个简单而好的解决方案。但我仍然不明白我的代码有什么问题(请发表您的评论作为答案)

标签: javascript asp.net .net asp.net-mvc-3 dom


【解决方案1】:

使用window.location = "/" 应该导航到网页的主机名,我认为这就是你想要做的。

您遇到的问题听起来像是您没有在要导航到的 URL 前面包含协议(http://https://)。

【讨论】:

    猜你喜欢
    • 2016-02-25
    • 1970-01-01
    • 2011-01-19
    • 2017-08-09
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多