Javascript 反应历史记录替换禁止返回按钮

Javascript 反应历史记录替换禁止返回按钮,javascript,reactjs,react-router,Javascript,Reactjs,React Router,我知道,从技术上讲,禁止用户点击浏览器的后退按钮是不可能的,但在我们的应用程序中,有时我们允许这种操作是没有意义的 例如,在“我的工作申请”应用程序中,申请后进入下一页 this.props.applyJob({ id: this.state.selectedAd_Id, applicant_id }).then(response => { if(response.value.status === 200){ const { id

我知道,从技术上讲,禁止用户点击浏览器的后退按钮是不可能的,但在我们的应用程序中,有时我们允许这种操作是没有意义的

例如,在“我的工作申请”应用程序中,申请后进入下一页

this.props.applyJob({
      id: this.state.selectedAd_Id,
      applicant_id
    }).then(response => {
      if(response.value.status === 200){
        const { id } = response.value.data.result
        this.props.history.replace(`/job/applied/${id}`)
      }
    })

but user still can go to the previous page which doesn't make sense, how to handle that?

您可以尝试某种onunload函数。如果用户单击您希望他们单击的按钮,则必须设置一个变量,以便他们不会收到警告消息

    var showAlert= 1; // this is set to it will show warning when leaving the page

    // if they click your next button add this to a function
    // showAlert =0;

window.onbeforeunload = function(){
    if (showAlert == 1)
  return 'Please use the Next and Previous buttons and not the browser back buttons to ensure your date is saved.';
};

您可以尝试某种onunload函数。如果用户单击您希望他们单击的按钮,则必须设置一个变量,以便他们不会收到警告消息

    var showAlert= 1; // this is set to it will show warning when leaving the page

    // if they click your next button add this to a function
    // showAlert =0;

window.onbeforeunload = function(){
    if (showAlert == 1)
  return 'Please use the Next and Previous buttons and not the browser back buttons to ensure your date is saved.';
};

这是黑客攻击浏览器,不推荐。这是黑客攻击浏览器,不推荐