0

I have an requirement where i want to call a java method when i click the browser forward or backward button.
Previously I implement the same by using Jquery and JavaScript,found easy to implement the same.Here is the link
But now my requirement is to do the same by executing a java method.And in this java method i will execute my own Business requirement.

My previous JavaScript code to implement the same is attached below

window.onbeforeunload = function (e) {
  ........
  ......
}

My previous JQuery code to implement the same is attached below

window.userInteractionTimeout = null;
window.userInteractionInHTMLArea = false;
window.onBrowserHistoryButtonClicked = null; // This will be your event handler for browser navigation buttons clicked

$(document).ready(function () {
    $(document).mousedown(function () {
        clearTimeout(window.userInteractionTimeout);
        window.userInteractionInHTMLArea = true;
        window.userInteractionTimeout = setTimeout(function () {
            window.userInteractionInHTMLArea = false;
        }, 500);
    });

$(document).keydown(function () {
        clearTimeout(window.userInteractionTimeout);
        window.userInteractionInHTMLArea = true;
        window.userInteractionTimeout = setTimeout(function () {
            window.userInteractionInHTMLArea = false;
        }, 500);
    });

    if (window.history && window.history.pushState) {
        $(window).on('popstate', function () {
            if (!window.userInteractionInHTMLArea) {
                // alert('Browser Back or Forward button was pressed.');
                 }
    if(window.onBrowserHistoryButtonClicked ){
    window.onBrowserHistoryButtonClicked ();
            }
        });
    }
});

I am using the following java version

  1. Java 1.8
  2. Eclipse IDE
  3. Maven

Any help is highly appreciated, all the best in advance,Good Luck!!!

  • You can still use javascript or jquery...just make an ajax call to trigger your java code. – brso05 Sep 20 '17 at 18:46
  • Have you looked at: https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/javascript.html ? – PM 77-1 Sep 20 '17 at 18:47

0 Answers0