sapdev logo background
sapdev logo sapdev logo
Comments

Javascript to capture when a user closes or leaves sap bsp html page




The only way to capture when a user closes or leaves a BSP web page is to use standard web development techniques to register a JavaScript function to the onbeforeunload and the onunload HTML events. This offers the ideal solution as the JS function you register could be an Ajax call, so allows you to insert ABAP code into the process as well as JavaScript.

Below I have included two solutions to this problem, the first only works in I.E. and the second should work in I.E. and other browsers such as Chrome etc. The reason I have included both is so you can see the difference and demonstrate why you need the more complicated second version. You also might find issues with your code down the line for specific versions of browsers and the extra knowledge of both methods may help you find a solution.

Basic solution to capture when user closes or leaves a page in I.E.

 <html>
 <head>
   <title>I.E. detecting browser close and leaving to new screen </title>

     <script type="text/javascript">

       var myclose = false;
       function ConfirmClose()
       {
          //within here it includes
          alert("Leaving Window!!");
          if (event.clientY < 0)
          {
             alert("Window has been closed!!");
             //on click of window close
             setTimeout('myclose=false',100);
             myclose=true;
          }
        }

        function HandleOnClose()
        {
          //after window has closed, note any alert that you put in here may not be displayed
          if (myclose==true)
          {
            alert("Window is closed!!");
            window.open("http://www.sapdev.co.uk/webapps/bsp/browser-closed.htm");
          }
        }

       </script>
 </head>


 <body onbeforeunload="ConfirmClose();" onunload="HandleOnClose();">

 <h4>Close browser!</h4>
<a href="http://www.sapdev.co.uk">test


Second solution to capture when user closes or leaves a page in all browsers
<html>
 <head>
   <title>Detecting browser close in IE and Chrome</title>

     <script type="text/javascript">

       if (window.addEventListener && !window.attachEvent) {
           window.addEventListener("unload", HandleOnClose, false);
           window.addEventListener("beforeunload", ConfirmClose_chrome, false);
       }
       else if (window.attachEvent) {
           window.attachEvent("onunload", HandleOnClose);
           window.attachEvent("onbeforeunload", ConfirmClose);
       }

       var myclose = false;
       function ConfirmClose()
       {
          //within here it includes
          alert("Leaving Window!!");
          if (event.clientY < 0)
          {
             //alert("Window has been closed!!");
             setTimeout( function () { myClose=false } , 100);
             //on click of window close
             setTimeout('myclose=false',100);
             myclose=true;
             return null;
          }
        }

       function ConfirmClose_chrome(e)
       {
          alert("Window has been closed!!");
          //on click of window close
          myclose=true;
          return null;
        }

        function HandleOnClose()
        {
          //after window has closed, note any alert that you put in here may not be displayed
          if (myclose==true)
          {
            alert("Window is closed!!");
            window.open('http://www.sapdev.co.uk/webapps/bsp/browser-closed.htm', '_blank');
          }
        }

       </script>
 </head>


 <body >

 <h4>Close browser!</h4>
<a href="http://www.sapdev.co.uk">test</a>

 </body>
 </html>

See this code in action




comments powered by Disqus