/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] using onbeforeunload

I know this event is a bit… strange, but I’m having problems with it. I know that onbeforeunload=”return ‘message'” will confirm if the user wants to change the page, however is there a way that I can choose whether or no to show the message during runtime, for instance:

onbeforeunload=”if(confirmleave==1){return ‘message’}”

like that…?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorJul 24.2008 — and [B]confirmleave [/B]would be [I]what[/I]?

Usually this kind of unbeforeunload code is used:
<i>
</i>window.onbeforeunload = function (evt) {
var message = 'Close?';
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}
Copy linkTweet thisAlerts:
@TaboauthorJul 24.2008 — and [B]confirmleave [/B]would be [I]what[/I]?

Usually this kind of unbeforeunload code is used:
<i>
</i>window.onbeforeunload = function (evt) {
var message = 'Close?';
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}
[/QUOTE]
I mean, lets say I have a long form on my page, when that the user begins to fill it out, I want it so that the user is alerted when they go off the page, but if the user hasn't started filling out the form, the user is not alerted when they leave. [I]confirmleave[/I] would be boolean, string or int which says weather or not the form has been changed.

I've done it now, based upon the code you gave, thanks a lot.
×

Success!

Help @Tabo 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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...