How do I remove a page from the browser history?

故事扮演 提交于 2019-12-03 05:22:37

I'm not sure if that can be done. But here is an idea how you could prevent that resubmit of the form.

You could insert a hidden input in your form which at the beginning would be empty. On submit you'll write a value in that field and make sure you check on every submit attempt if this field is empty.

If it is not empty on submit you know that the form was previously sent and you could warn the user.

Eugene Yokota
window.location.replace(URL);

window.location:

replace(url)

Replace the current document with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.

As a web application, you'll never have full control of the user's browser. Even if there was a way to instruct the browser to not store the page in history, which I doubt, you can't be sure it'll work. For example, a clever user could tweak an open-source browser to store every page in history, no matter what.

I think you should try to approach the problem from another angle. You could, for example, detect that it's the same form which is being forwarded and not send it to paypal the second time. The important thing is to do it server-side.

Perhaps you could set a cookie before submitting the form.

When the page is loaded, check for the existence of that cookie (meaning the form was already submitted). If found, instead of automatically submitting the form, automatically go back (window.history.back()) again.

I'm not sure if you can do this easily with PayPal integration, but the "Post / Redirect / Get" pattern can be used to address this problem

A useful Hint for some might be this...

window.history.go(-2);

particularly in the advent of a load failure warning popup.

You could simply programme your page not to submit, or to do something / navigate somewhere else, if window.referer is the Paypal page you are trying to avoid invoking a second time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!