1
我有,我想使用進行重定向的Javascript的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與舊的鏈接:我想http://localhost:31569/Event/2
被替換爲localhost:31569
。
我試過window.location =
,window.location.href =
和window.location.replace
,我得到了同樣的結果。
所以我需要知道:
- 是什麼這個問題的原因是什麼?
- 我該如何解決?
如果你想導航到URL的根目錄,爲什麼不只是'window.location =「/」'? –
@KianCross好工作:一個簡單而好的解決方案。但我仍然不明白我的代碼有什麼問題(請將您的評論發佈爲答案) –