21

I'm building a text editor in my website, the workflow is as follows.

  1. In /list, the user picks an entry they want to edit from a list which takes them to /edit/[article_id].
  2. The user does their work, and then clicks submit.
  3. The server processes the thing, and redirects them back to /edit/[article_id] which by now reflects the edited work. The server also activates a flash message on the page to indicate the edit was successful.

By this point, the user probably wants to go back to /list and clicks the browser's Back button. This will take them back to the editor repeatedly depending on how many times they submitted.

I've tried putting a Back button somewhere on the page, but a good many users simply ignore it.

I'd rather not make the submission posted via AJAX, since that would also affect the flash message system.

What I like to do, is replace the last entry on the history list when the user submits, without changing its length. Is it possible?

2 Answers 2

44

Try this

window.location.replace(url);

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.

4
  • 1
    So, I should use window.location.replace() in /list?
    – starleaf1
    Mar 21, 2017 at 5:13
  • 3
    url = window.location.href Jul 18, 2018 at 15:06
  • 1
    Excellent. As simple as it gets and exactly what is needed 🙏
    – mraxus
    Mar 11, 2021 at 13:38
  • 1
    wow. Thats cool
    – Amal
    Jan 18, 2022 at 11:44
-4

Use window.close():

close();

Note: the current tab is implied. This is equivalent:

window.close();

or you can specify a different window.

So:

function close_window() {
  if (confirm("Close Window?")) {
    close();
  }
}

with HTML:

<a href="javascript:close_window();">close</a>

or:

<a href="#" onclick="close_window();return false;">close</a>

You return false here to prevent the default behavior for the event. Otherwise the browser will attempt to go to that URL (which it obviously isn't).

1
  • That wasn't the question
    – CodeMonkey
    May 11, 2018 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.