Jump to content

Confirmation before closing of tab or browser


Ario.Paxaz

Recommended Posts

Where is MainmForm Script (it is not in ClientEvents) ?

 

I tried

 

  MyList:= TStringList.Create;
  MyList.Add('window.onbeforeunload = function() ');
  MyList.Add('{ ');
  MyList.Add('  return ""; ');
  MyList.Add('}; ');
  MainmForm.SetScript(MyList);
  MyList.Free;
 
But fails, please advise.
Link to comment
Share on other sites

Hi Andy -

 

The TUnimForm (Mobile Form) does not have a script property, but you can accomplish the same effect by adding an event handler for the window.afterCreate event in the ClientEvents->UniEvents list.

 

function window.afterCreate(sender)
{
  window.onbeforeunload = function () {
   return '';
  };
}
Link to comment
Share on other sites

Hi again Andy - I've done some digging... it looks like you may be out out of luck on this one... 

 

It seems that in the early days of the onbeforeunload implementation, it worked exactly like you would expect, but after constant abuse from unfriendly websites that would try to trap the user into staying on their page and hit them with popups, it was altered greatly, and not in the same way on every browser.  

 

see: 

 

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

 

 

"Since 25 May 2011, the HTML5 specification states that calls to window.alert()window.confirm(), and window.prompt() methods may be ignored during this event. See the HTML5 specification for more details.

 

Note also, that various browsers ignore the result of the event and do not ask the user for confirmation at all. The document will always be unloaded automatically. Firefox has a switch named dom.disable_beforeunload in about:config to enable this behaviour."

 

 

Basically, if the browser supports it at all, then it will most likely ignore anything you do in the event, display its own prompt to the user and only act on the result provided by hard user input.  I believe the mobile browsers for the most part ignore the event.

 

UniGui can only do what the browsers will allow.  Sorry. - As a developer who understands the desire to provide users with a safety net from accidentally closing the browser/tab and loosing data, I am saddened, but I do understand the reason it was altered. 

Link to comment
Share on other sites

Thanks Farshad - I had actually looked into that for Andy as well... 

 

on my samsung phone, using chrome, this does indeed handle the case of the user hitting back button (though not consistently), but it doesn't help with closing the tab or browser ... I don't believe this is possible as the browser doesn't support it.  UniGui can only do so much.  And it makes sense... Giving an app or a website the power to not shut down when the user is instructing it to, intentionally or otherwise, is too much power.  Maybe just have a warning on your main or login screen that doing so may result in lost data.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...