<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: Browser back button - any workarounds ?</title><link>https://answers.axonivy.com/questions/4402/browser-back-button-any-workarounds</link><description>&lt;p&gt;Dear Ivy Team&lt;/p&gt;
&lt;p&gt;It seems that Ivy can't handle by default the browser back button if the UI is related to a process. Your proposed solution is to suppress the back button. Imho this is not an acceptable solution especially if everything else reacts correctly to the back button.&lt;/p&gt;
&lt;p&gt;I would assume/expect that a Ivy process which is designed with multiple "back paths" should be able to support some kind of a "back button" behavior.&lt;/p&gt;
&lt;p&gt;Regards
John Moser&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/4402/browser-back-button-any-workarounds" rel="self"></atom:link><language>en</language><lastBuildDate>Tue, 10 Mar 2020 12:15:26 -0400</lastBuildDate><item><title>Answer by vinh_</title><link>https://answers.axonivy.com/questions/4402/browser-back-button-any-workarounds/4449</link><description>&lt;p&gt;I worked for this issue recently. I could share the idea. We will use "load" and "popstate" event to detect and handle the back button in general and show a custom dialog, you will have full control of the dialog, then you can disable the back button completely.&lt;/p&gt;
&lt;p&gt;Refer the load event documentation from Mozilla: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event"&gt;https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Idea get from an answer on StackOverflow: &lt;a href="https://stackoverflow.com/questions/18141729/browsers-back-button-click-with-confirm-box"&gt;https://stackoverflow.com/questions/18141729/browsers-back-button-click-with-confirm-box&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;JavaScript&lt;/h1&gt;
&lt;pre&gt;&lt;code&gt;var exitPageConfirmDialog = {
  confirmation: undefined,
  enabled: true,
  enable: function() { this.enabled = true; },
  disable: function() { this.enabled = false; },
  show: function() { $('#exitPageConfirmDialog').show(); },
  hide: function() { $('#exitPageConfirmDialog').hide(); },
  showCustomConfirmDialog: function() {
    // creates new history entry with same URL for the first popstate could work
    history.pushState(null, null, null);
    window.addEventListener('popstate', function() {
      if (exitPageConfirmDialog.enabled === true) {
        exitPageConfirmDialog.confirmation = $.Deferred();
        exitPageConfirmDialog.confirmation.promise()
            .then(function resolve() {
              exitPageConfirmDialog.disable();
              history.back();
            }, function reject() {
              exitPageConfirmDialog.enable();
              history.pushState(null, null, null);
            });
        exitPageConfirmDialog.show();
      }
    });
  },
  init: function() {
    $(document).ready(function() {
      // Display custom confirm dialog, when user click on back button.
      window.removeEventListener('load', exitPageConfirmDialog.showCustomConfirmDialog);
      window.addEventListener('load', exitPageConfirmDialog.showCustomConfirmDialog);
    });
  }
};
&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;HTML&lt;/h1&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;div id="exitPageConfirmDialog" style="display: none; background-color: grey;"&amp;gt;
  &amp;lt;p&amp;gt;Do you want to go back?&amp;lt;/p&amp;gt;
  &amp;lt;p&amp;gt;&amp;lt;button onclick="exitPageConfirmDialog.hide(); exitPageConfirmDialog.confirmation.resolve();"&amp;gt;YES&amp;lt;/button&amp;gt;&amp;lt;/p&amp;gt;
  &amp;lt;p&amp;gt;&amp;lt;button onclick="exitPageConfirmDialog.hide(); exitPageConfirmDialog.confirmation.reject();"&amp;gt;NO&amp;lt;/button&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt; script&amp;gt;
  exitPageConfirmDialog.init();
&amp;lt; /script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You should add onclick in YES/NO button, this editor prevent me adding those attribute:&lt;/p&gt;
&lt;p&gt;YES-onclick: exitPageConfirmDialog.hide(); exitPageConfirmDialog.confirmation.resolve();&lt;/p&gt;
&lt;p&gt;NO-onclick: exitPageConfirmDialog.hide(); exitPageConfirmDialog.confirmation.reject();&lt;/p&gt;
&lt;h1&gt;Testing&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;create 2 pages, the second page will have this script, and an input to interact with (some browser like Chrome will not execute event unless you interact with some field inside the page).&lt;/li&gt;
&lt;li&gt;open the first page, then click on a link to navigate to the second page.&lt;/li&gt;
&lt;li&gt;type some thing in the input field&lt;/li&gt;
&lt;li&gt;click on Back button, you will see the confirm message and YES/NO button (you should use style="display: none" on the dialog, I could not put it in this answer because of the editor).&lt;/li&gt;
&lt;li&gt;When click YES, you will go back. When click NO, you will stay. If you remove the YES button, you will always keep user stay on the page.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In case you want temporary disable the dialog, use&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;exitPageConfirmDialog.disable();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Enable again&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;exitPageConfirmDialog.enable();
&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Known issue&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;Confirm dialog does not show if you are not interact with the page.&lt;/li&gt;
&lt;/ol&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">vinh_</dc:creator><pubDate>Tue, 10 Mar 2020 12:15:26 -0400</pubDate><guid>https://answers.axonivy.com/questions/4402/browser-back-button-any-workarounds/4449</guid></item><item><title>Answer by Reguel Wermelinger</title><link>https://answers.axonivy.com/questions/4402/browser-back-button-any-workarounds/4444</link><description>&lt;p&gt;The hack provided here works like a charm on Chromium: &lt;a href="https://support.google.com/chrome/thread/8721521?msgid=10849294"&gt;https://support.google.com/chrome/thread/8721521?msgid=10849294&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;tested with: Chromium Version 80.0.3987.87 (Official Build) Built on Ubuntu and Axon.ivy 8.0 LTS&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;script type = "text/javascript" &amp;gt; history.pushState(null, null, location.href); history.back(); history.forward(); window.onpopstate = function () { history.go(1); }; &amp;lt;/script&amp;gt;​
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If added this in my main layout xhtml and now the back button is no longer doing any harm in any of my dialogs.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Reguel Wermelinger</dc:creator><pubDate>Wed, 04 Mar 2020 10:41:01 -0500</pubDate><guid>https://answers.axonivy.com/questions/4402/browser-back-button-any-workarounds/4444</guid></item></channel></rss>