There's no harm in trying (a browser might decide that the page is allowed to—perhaps if your user has visited you a lot before) but it's unlikely to work. As per my post, maybe consider using sendBeacon if there's some information you want to exfiltrate from the page before it closes...
Even calling alert() is often disallowed in background pages (or at least delayed until the page gets focus again).
I have passed complex page which has decades tests.
After passing this page, I get into the test result page.
If I click back button without any interaction in browser, then it goes to test page without confirmation, and I lost test result and should pass complex test again.
Thanks for good post.
I have one error, I want your help.
beforeunload function does not work if there is no user interaction.
It causes following error:
Blocked attempt to show a 'beforeunload' confirmation panel for a frame that never had a user gesture since its load.
How to show warning message even no user interaction?
Hoping your help.
Thanks
Good observation, although I actually mentioned this in the article:
This makes sense—a page that I've put into a background tab and then later closed shouldn't be able to get my attention!
Thanks for your reply.
Little trick.
So, is it impossible to show warning message if there is no user interaction?
There's no harm in trying (a browser might decide that the page is allowed to—perhaps if your user has visited you a lot before) but it's unlikely to work. As per my post, maybe consider using
sendBeacon
if there's some information you want to exfiltrate from the page before it closes...Even calling
alert()
is often disallowed in background pages (or at least delayed until the page gets focus again).Let us assume.
I have passed complex page which has decades tests.
After passing this page, I get into the test result page.
If I click back button without any interaction in browser, then it goes to test page without confirmation, and I lost test result and should pass complex test again.
I hope it can be resolved.
Any Idea?
Yes, that will go "back", but there are lots of other ways to store state (like your test results) that don't rely on the user keeping a page open.
e.g., indexdb, local storage, building a SPA where 'back' is handled by your code, etc ...
Understand.
Thanks for your kindly help.
Hoping your good post. :)
Do you think it is impossible to implement warning message without user interaction?
Yes, I think it's impossible. Allowing an annoying popup to be generated without the user ever using the page is against what browsers are aiming for.
Can you explain about "against what browser are aiming for"?
Sorry for annoying. :)