1
0
-1

We want to detect and act upon a browser close event. Specifically, they want to disconnect from a particular backend service, so that the next person using the same machine will have to log in again to that service.

This appears to be difficult problem to solve in a cross-browser way.

https://stackoverflow.com/questions/3888902/detect-browser-or-tab-closing/3888938

https://stackoverflow.com/questions/20853142/trying-to-detect-browser-close-event

However, we have a closed user environment and only one version of one browser, so it’s a more contained problem.

In reality, all we need to do is to write a handler for the onbeforeunload event.

https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload

Does anyone have a suggestion of the best way to implement this in Maestro?

  • In the template?
  • Using some custom javascript/jquery?
  • Adding a custom rule type somewhere?
    CommentAdd your comment...

    1 answer

    1.  
      1
      0
      -1

      Here is some code from a Form Load rule I have to make a final call to a service before the browser closes:

      vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

      Form.data.$config = DynamicData.config;

      $(window).on('beforeunload', function (e) {
        // This function will call a dynamic data service before the window is closed
        var params = {
          sfmServiceName: "All Subs",
          sfmOperationName: "dynamicData",
          sfmFormCode: $("form").scope().data.$config.formCode,
          sfmRequestKey: $("form").scope().data.$config.requestKey
        }

      // The ajax call is made synchronously, otherwise the window will be closed before callbacks complete
        $.ajax({
          async: false,
          type: "POST",
          url: $("form").scope().data.$config.url,
          data: params,
          contentType: "application/x-www-form-urlencoded",
          dataType: "json",
          timeout: 60000,
          success: function (response) {
              var r = response;
          },
          error: function (jq, status, message) {
              var e = message;
          }
        });
      });

      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

      The code doesn't do anything useful, it just calls a TM service and catches the response as a proof-of-concept.

      The trick is to make the AJAX call synchronously, because the JavaScript engine only gives your code one final loop to execute.  As soon as you make any async calls, the engine never comes back again to let you see responses from promises.

        CommentAdd your comment...