我目前正在设置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