我有一个 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;
我得到的输出:
问题是新的 URL 与旧的 URL 连接在一起:我想要 http://localhost:31569/Event/2
替换为localhost:31569
.
我试过window.location =
, window.location.href =
和window.location.replace
我得到了相同的结果。
所以我需要知道:
- 这个问题的原因是什么?
- 我该如何修复它?
最佳答案
使用 window.location = "/"
应该导航到网页的主机名,我认为这就是您想要做的。
您遇到的问题听起来像是您没有在要导航的 URL 前面包含协议(protocol)(http://
或 https://
)到。
关于javascript - 使用 JavaScript 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37764707/