Jump to content

Best way to detect tab close


fiorilli

Recommended Posts

Hello, I need to detect when the browser or my site's tab is closed, but I couldn't find an effective way. Currently I use 


procedure TMainForm.UniFormAjaxEvent(Sender: TComponent; EventName: string; Params: TUniStrings);
begin
  if EventName = 'bclose' then
   statement
end;
 
With this I can detect the closing of the browser, but not the closing of the tab. What would be the best way to solve this case? Also, I would like to know if there is any documentation on unigui's event triggers such as "bclose". 

Some topics I've already consulted are:

http://forums.unigui.com/index.php?/topic/12182-browser-or-tab-close/
http://forums.unigui.com/index.php?/topic/2967-kill-a-session-when-browser-is-closed/

Link to comment
Share on other sites

2 hours ago, fiorilli said:

Hello, I need to detect when the browser or my site's tab is closed, but I couldn't find an effective way. Currently I use 


procedure TMainForm.UniFormAjaxEvent(Sender: TComponent; EventName: string; Params: TUniStrings);
begin
  if EventName = 'bclose' then
   statement
end;
 
With this I can detect the closing of the browser, but not the closing of the tab. What would be the best way to solve this case? Also, I would like to know if there is any documentation on unigui's event triggers such as "bclose". 

Some topics I've already consulted are:

http://forums.unigui.com/index.php?/topic/12182-browser-or-tab-close/
http://forums.unigui.com/index.php?/topic/2967-kill-a-session-when-browser-is-closed/

Hello

Please try below code.

If you have LoginForm

Loginform -> Script

If you don't have LoginForm

Mainform -> Script

window.addEventListener('beforeunload', function(e) {
   ajaxRequest(uniVars._mFrm_,"UniLogout");
});
UniMainModule -> OnHandleRequest
procedure TUniMainModule.UniGUIMainModuleHandleRequest(ASession: TObject;
  var Handled: Boolean);
var
  Event : string;
begin
  if (ASession<>nil)  then
  begin
    if (TUniGUISession(ASession).IsAjax) then
    begin
      try
        Event:= TUniGUISession(ASession).ARequest.Params.Values['evt'];
        if (Event = 'UniLogout')  or (Event = 'bclose') then
          // Your Logic Here
          // UniSession.Terminate;
      except
      end;
    end;
  end;
end;

 

Link to comment
Share on other sites

45 minutes ago, Hayri ASLAN said:

Hello

Please try below code.

If you have LoginForm

Loginform -> Script

If you don't have LoginForm

Mainform -> Script


window.addEventListener('beforeunload', function(e) {
   ajaxRequest(uniVars._mFrm_,"UniLogout");
});

UniMainModule -> OnHandleRequest

procedure TUniMainModule.UniGUIMainModuleHandleRequest(ASession: TObject;
  var Handled: Boolean);
var
  Event : string;
begin
  if (ASession<>nil)  then
  begin
    if (TUniGUISession(ASession).IsAjax) then
    begin
      try
        Event:= TUniGUISession(ASession).ARequest.Params.Values['evt'];
        if (Event = 'UniLogout')  or (Event = 'bclose') then
          // Your Logic Here
          // UniSession.Terminate;
      except
      end;
    end;
  end;
end;

 

Hello, it didn't work, I put it in my login form, but after logging in and closing the tab the event is not called

Link to comment
Share on other sites

You need to catch it in the MainModule:

procedure TUniMainModule.UniGUIMainModuleHandleRequest(ASession: TObject;
  var Handled: Boolean);
var
  Event : string;
begin
  if (ASession<>nil)  then
  begin
    if TUniGUISession(ASession).IsAjax then
    begin
      try
        Event:= TUniGUISession(ASession).ARequest.Params.Values['evt'];
        if (Event = 'bclose') then

//Do something..
      except
      end;
    end;
  end;
end;

Link to comment
Share on other sites

19 hours ago, fiorilli said:

Hello, it didn't work, I put it in my login form, but after logging in and closing the tab the event is not called

Hello

I tested below scenarios and all of them works
-close the browser
-close the tab
-navigate to another webpage
-hit refresh

 

Which version are you using?

Link to comment
Share on other sites

1 hour ago, irigsoft said:

hello, I try it with mobile google chrome and is not work.  Did You test it by this way ?

Professional 1524

Hello

Are you using mobile form and mobile browser or desktop form and mobile browser?

Please be specific on your setup so I can test like how you are testing.

Link to comment
Share on other sites

39 minutes ago, Hayri ASLAN said:

I don't think we have a solution for mobile browsers yet. I searched and the only thing that people are using is window.blue event. But it is not accurate as it is firing when you open another tab too,

Thank you.

Now i understand my problem.

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...