Please see this minimum example
When using location.href
to navigate to a new page, I can easily go to previous thanks to the browser.
<button>Can Go To Previous Page</button>
<script>
document.querySelector("button").addEventListener("click", () => {
window.location.href = "https://google.com";
});
</script>
And I can easily disable the behavior simply using location.replace
<button>Can Not Go Back Anymore!</button>
<script>
document.querySelector("button").addEventListener("click", () => {
window.location.replace("https://google.com");
});
</script>
Now, how can I make form.submit()
acts like location.replace
?
Because you can still go back to the previous page when you submitting a form.
Is it possible to do this?
<form action="/action_page.php">
<label>
First name:
<input type="text" id="firstname" name="firstname" />
</label>
<button>Submit, Can still go back here</button>
</form>
<script>
document.querySelector("button").addEventListener("click", () => {
const form = document.querySelector("form");
form.submit();
});
</script>