如果用户在 url foo.com/#/veryLongToken 并且 response.redirectUrl
是 foo.com/#/veryLongToken/resultToken ,在第一个示例中,当页面重新加载时,我们最终到达 foo.com/#/veryLongToken。
在第二个示例中,我们最终到达了 foo.com/#/veryLongToken/resultToken,这是我们所期望的。
更多信息;
- 网址很长
- 使用 AngularJS 窗口包装器 $window
- 已在 Chrome 和 Safari 中测试,均显示相同的行为
不起作用:
function successHandler(response) {
$window.location.href = response.redirectUrl;
/* redirectUrl might contain only changes to the hash-fragment
which is why we force a reload here */
$window.location.reload();
}
作品;
function successHandler(response) {
$window.location.hash = response.redirectUrl.substr(response.redirectUrl.indexOf('#'));
$window.location.reload();
}
我真的很想知道为什么第一个示例不起作用。有什么想法吗?
response.redirectUrl
是
最佳答案
作为 URL 中的 # 标记 anchor ,更改它不会触发页面加载,因为您的 bas url (foo.com/) 保持不变。 window.location.href 不能处理 anchor 更改。
如果你想改变“ anchor ”部分,你必须使用 window.location.hash 来检测它(如你的第二个例子)
关于javascript - 设置 window.location.href 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31137809/