<p>So the SMEs at my current place of employment want to try and disable the back button for certain pages.  We have a page where the user makes some selections and submits them to be processed.  In some instances they have to enter a comment on another page.  </p>

<p>What the users have figured out is that they don't have to enter a comment if they submit the information and go to the page with the comment and then hit the back button to return to the previous page.  </p>

<p>I know there are several different solutions to this (and many of them are far more elegant then disabling the back button), but this is what I'm left with.  Is it possible to prevent someone from going back to the previous page through altering the behavior of the back button.  (like a submit -> return false sorta thing).  </p>

<p>Due to double posting information I can't have it return to the previous page and then move to the current one.  I can only have it not direct away from the current page.  I Googled it, but I only saw posts saying that it will always return to the previous page.  I was hoping that someone has some mad kung foo js skills that can make this possible.</p>

<p>I understand that everyone says this is a bad idea, and I agree, but sometimes you just have to do what you're told.</p>

[{'answer_id': 54554, 'author': 'Guy Starbuck', 'author_id': 2194, 'author_profile': 'https://Stackoverflow.com/users/2194', 'pm_score': 1, 'selected': False, 'text': '<p>Disabling the back button seems kind of a "brute force" approach.</p>\n\n<p>Another option would be that you could jump out to a modal dialog that doesn\'t have command buttons, walk users through the workflow, and close the dialog when the process is complete.</p>\n'}, {'answer_id': 54556, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 2, 'selected': False, 'text': '<p>Could you move the comment to the previous page and make it a required field there?</p>\n\n<p>Disabling the back button <strong>will not work.</strong></p>\n'}, {'answer_id': 54560, 'author': 'Brian Warshaw', 'author_id': 1344, 'author_profile': 'https://Stackoverflow.com/users/1344', 'pm_score': 2, 'selected': False, 'text': "<p>Do you have access to the server-side source code? If so, you can put a check on the first page that redirects to the second page if the information has been submitted already (you'll need to use sessions for this, obviously). At a former job, this is how we handled multi-step applications (application as in application for acceptance).</p>\n"}, {'answer_id': 54563, 'author': 'Matt Dawdy', 'author_id': 232, 'author_profile': 'https://Stackoverflow.com/users/232', 'pm_score': 0, 'selected': False, 'text': '<p>There simply is no reliable way to do this.  You cannot guarantee that 100% of the time you can stop the user from doing this.</p>\n\n<p>With that in mind, is it worth going to extremely exotic solutions to disable "most" of the time?  That\'s for you to decide.</p>\n\n<p>Good luck.</p>\n'}, {'answer_id': 54564, 'author': 'pkaeding', 'author_id': 4257, 'author_profile': 'https://Stackoverflow.com/users/4257', 'pm_score': 2, 'selected': False, 'text': "<p>Because of the security isolation of javascript in the browser, you cannot change what the back button does.</p>\n\n<p>Perhaps you could store something in the user's session that indicates that a comment is needed, and have any page in the app that the user tries to load redirect to the comment page?</p>\n\n<p>What if the user closes their browser when he/she gets tot he comment page?</p>\n\n<p>I know that you have not been given a choice here, but since what they are asking for seems to be impossible...</p>\n\n<p>Perhaps you could just not consider the item as completed until the user enters comments.  Thus, you would need to keep track of both in-progress items and completed items, and make that distinction in the UI, but this might be the most robust method.</p>\n\n<p>Or just put the comment field on the form page?</p>\n"}, {'answer_id': 54571, 'author': 'David Mohundro', 'author_id': 4570, 'author_profile': 'https://Stackoverflow.com/users/4570', 'pm_score': 2, 'selected': False, 'text': "<p>I've seen this before:</p>\n\n<pre><code>window.onBack = history.forward();\n</code></pre>\n\n<p>It is most definitely a dirty hack and, if at all possible, I would attempt to not disable the back button. And the user can probably still get around it quite easily. And depending on caching, there is no telling if the server code will be processed or if the cached page with JavaScript will run first. </p>\n\n<p>So, yeah, use at your own risk :)</p>\n"}, {'answer_id': 54574, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 3, 'selected': False, 'text': "<p>Nah, you're doomed. Even if you pop the page up in some different browser and hid the back button, there's always the Backspace key.</p>\n\n<p>The problem with marketing guys and analyst types is that some of them do not understand the fundamental concept of the web being stateless. They do not understand that the page is totally, totally unaware of the browser using it and absolute control of the browser is totally outside the capability of web pages.</p>\n\n<p>The best way to discourage your users to hit the back button is to make sure that your page loses <em>all</em> its data when they press back, e.g., the comment page is the only point where the data can be saved, and if they do press the back button they have to do <em>everything</em> all over again (think along the lines of pragma: nocache).</p>\n\n<p>Users will complain, sure, but they are the reason that this godforsaken requirement exists, right?</p>\n"}, {'answer_id': 54577, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 5, 'selected': False, 'text': "<p>Don't do this, just don't. It's bad interface design and forces the user's browser to behave in a way that they don't expect.</p>\n\n<p>I would regard any script that successfully stopped my back button from working to be a hack, and I would expect the IE team to release a security-fix for it.</p>\n\n<p>The back button is part of their program interface, not your website.</p>\n\n<p>In your specific case I think the best bet is to add an unload event to the page that warns the user if they haven't completed the form. The back button would be unaffected and the user would be warned of their action.</p>\n"}, {'answer_id': 54627, 'author': 'Peter Hilton', 'author_id': 2670, 'author_profile': 'https://Stackoverflow.com/users/2670', 'pm_score': 2, 'selected': False, 'text': "<blockquote>\n  <p>What the users have figured out is\n  that they don't have to enter a\n  comment if they submit the information\n  and go to the page with the comment\n  and then hit the back button to return\n  to the previous page.</p>\n</blockquote>\n\n<p>Then they are probably also smart enough to type <em>'no comment'</em> into the comments field.</p>\n\n<p>You can try to force people to add comments, but you will probably just end up with bad unusable software, annoyed users, and still not get comments. This is usually a good time to take a step back and reconsider what you are doing from the users' point of view.</p>\n"}, {'answer_id': 294001, 'author': 'Kornel', 'author_id': 27009, 'author_profile': 'https://Stackoverflow.com/users/27009', 'pm_score': 1, 'selected': False, 'text': '<p>You should secure your application against double submission instead of breaking user interface to hide the bug.</p>\n'}, {'answer_id': 720015, 'author': 'Andy', 'author_id': 87436, 'author_profile': 'https://Stackoverflow.com/users/87436', 'pm_score': 0, 'selected': False, 'text': '<p>AS a simple solution: try this one. Insert an update panel and a button in there and use javascript to hide it and then press it on page load. Yes I understand that it will cause your page to post back on load and may not work if javascript is disabled but certainly will help you achieve a half decent response to the back button issue. Andy</p>\n'}, {'answer_id': 5881224, 'author': 'Yossi Shasho', 'author_id': 437019, 'author_profile': 'https://Stackoverflow.com/users/437019', 'pm_score': 2, 'selected': False, 'text': '<p>I came up with a little hack that disables the back button using JavaScript. I checked it on chrome 10, firefox 3.6 and IE9:</p>\n\n<pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;\n&lt;html xmlns="http://www.w3.org/1999/xhtml" &gt;\n&lt;title&gt;Untitled Page&lt;/title&gt;\n&lt;script type = "text/javascript" &gt;\nfunction changeHashOnLoad() {\n     window.location.href += "#";\n     setTimeout("changeHashAgain()", "50"); \n}\n\nfunction changeHashAgain() {\n  window.location.href += "1";\n}\n// If you want to skip the auto-positioning at the top of browser window,you can add the below code:\nwindow.location.hash=\' \';\nvar storedHash = window.location.hash;\nwindow.setInterval(function () {\n    if (window.location.hash != storedHash) {\n         window.location.hash = storedHash;\n    }\n}, 50);\n\n\n&lt;/script&gt;\n&lt;/head&gt;\n&lt;body onload="changeHashOnLoad(); "&gt;\nTry to hit the back button!\n&lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n'}, {'answer_id': 11268009, 'author': 'Webveloper', 'author_id': 424671, 'author_profile': 'https://Stackoverflow.com/users/424671', 'pm_score': 0, 'selected': False, 'text': "<p>You can prevent them from going back to the previous page. <code>location.replace()</code> replaces the current page's history entry with a new page, so...</p>\n\n<p>page1.html: user clicks a link that goes to page2.html</p>\n\n<p>page2.html: user clicks a link that calls <code>location.replace('page3.html');</code></p>\n\n<p>page3.html: user clicks back button and goes to page1.html</p>\n\n<p>This may not fit well with doing a POST, but you could post the data to a web service via AJAX, then call location.replace()</p>\n"}, {'answer_id': 30591489, 'author': 'Sam Watkins', 'author_id': 218294, 'author_profile': 'https://Stackoverflow.com/users/218294', 'pm_score': 0, 'selected': False, 'text': "<p>If you are starting a new web app from scratch, or you have enough time to rework your app, you can use JavaScript and AJAX to avoid the browser's history, back, and forward functions.</p>\n\n<ul>\n<li>Open your app in a new window, either before or immediately after login.</li>\n<li>If you wish, use window options to hide the nav bar (with the back and forward buttons).</li>\n<li>Use AJAX for all server requests, without ever changing the window's location URL.</li>\n<li>Use a simple web API to get data and perform actions, and render the app using JavaScript.</li>\n<li>There will be only one URL in the window's history.</li>\n<li>The back and forward buttons will do nothing.</li>\n<li>The window can be closed automatically on logging out.</li>\n<li>No information is leaked to the browser history, which can help with security.</li>\n</ul>\n\n<p>This technique answers the question, but it also contradicts best practice in several ways:</p>\n\n<ul>\n<li>The back and forward buttons should behave as expected.</li>\n<li>An app should not open new browser windows.</li>\n<li>An app should still function without JavaScript.</li>\n</ul>\n\n<p>Please carefully consider your requirements and your users before using this technique.</p>\n"}, {'answer_id': 33186526, 'author': 'Denis Chenu', 'author_id': 2239406, 'author_profile': 'https://Stackoverflow.com/users/2239406', 'pm_score': 0, 'selected': False, 'text': '<p>I don\'t see this solution :</p>\n\n<p><div class="snippet" data-lang="js" data-hide="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>function removeBack(){\r\n window.location.hash="nbb";\r\n window.location.hash="";\r\n window.onhashchange=function(){window.location.hash="";}\r\n}</code></pre>\r\n</div>\r\n</div>\r\n</p>\n'}]
['https://Stackoverflow.com/questions/54539', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1942/']