我目前正在設置window.location.pathname屬性以將用戶重定向到相對URL。新的URL有參數,這樣的JavaScript的線路是這樣的:設置JavaScript window.location
window.location.pathname = window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/') + 1) + 'myPage.xhtml?u=' + selected_user.Username;
這是成功的在Firefox,Chrome的但是編碼問號「%3F」和隨後失敗的請求。
我不確定我是否正確使用window.location。我是否需要使用window.location的屬性,如pathname或href?我發現只要設置了一個屬性,位置就會重新加載,所以例如搜索和路徑名屬性不能單獨設置。可以直接設置window.location嗎?我只需要用參數設置一個相對URL。
感謝您的詳細解釋。我更改了我的代碼以使用href屬性,並且還調用encodeURIComponent()。 – Mark 2010-09-04 20:16:50
請注意[對於大多數情況](http://stackoverflow.com/questions/6725890/window-location-host-vs-window-location-hostname-and-cross-browser-compatibility),你想使用' location.host'而不是'location.hostname'。總之,這是因爲你的代碼在某些時候可能運行在80以外的端口上的服務器上。 – 2013-09-28 21:12:44
請注意,在源代碼爲iframe的(罕見)情況下,它需要執行window.parent.location .. ... – 2015-07-15 13:29:31