javascript - JavaScript push 和 replaceState 导致前进按钮中断
问题描述
我基本上完成了我的网络应用程序,除了当我尝试使用历史 API 时它会禁用我的前进按钮功能。后退按钮效果很好。据我所知,我没有在 onpopstate 事件侦听器中使用 pushState。请任何人都可以帮助我理解为什么状态消失并且您无法单击前进?我附上相关代码。
if (document.querySelector('#weather-form')) {
history.pushState({ city: 'none_yet' }, ``, ``);
}
window.onpopstate = function(event) {
var state = event.state;
if (state !== null) {
console.log(`State 2: ${state['city']}, Window.location: ${window.location}`);
window.location.reload();
} else {
console.log(`State 3: ${state}, Window.location: ${window.location}`);
window.location.reload();
}
}
然后在每当用户提交包含其位置的表单时调用的函数中:
function get_data(city, time, units) {
let cityObj = {
city: city,
time: time,
units: units
};
fetch('http://127.0.0.1:8000/get_data', {
method: 'POST',
body: JSON.stringify(cityObj)
})
.then(response => response.json())
.then(response => {
console.log(response);
if (response["Error"]) {
let error_message = document.createElement('h5');
error_message.id = "error";
error_message.innerHTML = `The location input '${city}' is invalid. Please try again.`;
document.querySelector('#main').append(error_message);
} else {
history.replaceState({ city: city }, ``, `/city/${city}/`);
非常感谢任何帮助/解释。谢谢。
解决方案
推荐阅读
- python - 从float类型转换为int类型后,为什么python中的数字都变成了-1?
- swift - 展开和折叠 UICollectionViewController 中选定部分的单元格
- dart - 在 Flutter 中实现确定的 CircularProgressIndicator?
- python - AttributeError:'str'对象在获取json对象键时没有属性'keys'错误
- angular - Angular 6 应用程序的多子域主机创建
- ios - DateFormatter 返回的字符串语言不正确
- python - 在python中具有相同类别的固定颜色的条形图
- docker - kakfa 中的 JDBC sql server 驱动程序安装 - image wurstmeister/kafka
- powershell - 获取内容匹配文件
- image - 上传图片后,React Native 中仅 iOS 的图片 uri 为空