使用javascript重定向
我有一个ASP.NET MVC 5应用程序,我想在其中使用Javascript执行重定向:使用javascript重定向,javascript,asp.net,.net,asp.net-mvc-3,dom,Javascript,Asp.net,.net,Asp.net Mvc 3,Dom,我有一个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 <
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;
var sPageURL=decodeURIComponent(window.location);
日志(sPageURL);
var lengthUrl=sPageURL.split('/').length;
var NewUrl='';
sPageURL.split('/').forEach(函数(项,索引){
如果(索引
我得到的结果是:
问题是新URL与旧URL连接在一起:我想http://localhost:31569/Event/2
替换为localhost:31569
我尝试了window.location=
,window.location.href=
和window.location.replace
,得到了相同的结果
所以我需要知道:
使用
window.location=“/”
应该导航到网页的主机名,我认为这正是您要做的
您遇到的问题听起来像是您没有在要导航到的URL前面包含协议(
http://
或https://
)。如果您想导航到URL的根目录,为什么不执行window.location=“/”
?@KianCross good:一个简单而好的解决方案。但我仍然不明白我的代码有什么问题(请将您的评论作为答案发布)