/    Sign up×
Community /Pin to ProfileBookmark

alert on window close

I have a little problem.
I have a page with link: “click here to fill the form”,
You click and new pop-up window appears,
This new window have form and 2 buttons below: “send form”, “cancel”.

I want if user closes window himself (with window close button in upper right corner of the window) this happens:
<body onbeforeunload=”alert(‘Please use CANCEL button’); return false;”>

but if user press “cancel” in form this window is close immediately.

My problem/question is:
How to check if user used close window icon and if so – write alert.

thanks in advance
adam

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@FangDec 10.2007 — You can't distinguish which browser control closes the browser, window or tab.
Copy linkTweet thisAlerts:
@KorDec 10.2007 — Like this?
<i>
</i>&lt;script type="text/javascript"&gt;
onbeforeunload = function (evt) {
var message = 'Do you want to leave this page?';
evt=(typeof evt == 'undefined')?window.event:evt;
evt?evt.returnValue = message:null;
return message;
}
&lt;/script&gt;
×

Success!

Help @axeld spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...