/    Sign up×
Community /Pin to ProfileBookmark

Ok Cancel alert on exit webpage

i have a page where i wanted the user to confirm if she/he really wanted to leave before going to another page. this page contains a form and there is buttons on that particular form. user who click on the button will not hav such message prompt out. how can i archieve those two condition ??

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@winsonleeauthorOct 19.2005 — A default msg is being used for the on exist alert. I would like to know if i am able to customise msg so that i can type my own msg for the on exit ??
[code=html]

<script type="text/javascript"><!--
var actuallysubmitted=false;
function unloadCheck() {
//onunload doesn't work for this; you have to use onbeforeunload
// IE4+/Win, Mozilla 1.7a+, Netscape 7.2+, and Firefox support onbeforeunload.
if(!actuallysubmitted){
var f =document.myform;
flds = f.elements;
for(var i=0;i<flds.length;i++) {
if(flds[i].value != flds[i].defaultValue) {
return false;
}
}
}
}
// -->
</script>

[/code]
Copy linkTweet thisAlerts:
@KravvitzOct 19.2005 — Change
return false;
to
return 'your custom message';
Copy linkTweet thisAlerts:
@winsonleeauthorOct 20.2005 — it is so weird. when i click on the link which it suppose to call the test function, but ended up the unloadCheck is called before calling the test function. i wonder why this happends ?

[code=html]


<script type="text/javascript"><!--
var actuallysubmitted=false;
function unloadCheck() {
//onunload doesn't work for this; you have to use onbeforeunload
// IE4+/Win, Mozilla 1.7a+, Netscape 7.2+, and Firefox support onbeforeunload.
if(!actuallysubmitted){
var f =document.myform;
flds = f.elements;
for(var i=0;i<flds.length;i++) {
if(flds[i].value != flds[i].defaultValue) {
return 'Leaving this page will result you to lost all the issues entered during this meeting.';
}
}
}
}
// -->

function test(){

alert('abc');

}
</script>


<body onbeforeunload="return unloadCheck();">


<A HREF="javascript:test();">

[/code]
Copy linkTweet thisAlerts:
@KravvitzOct 20.2005 — It's because the onbeforeunload is triggered before the href is processed because normally the href is a URL to be followed, thus often will cause the page to unload.

You may want to use onclick instead of href="javascript:...".
×

Success!

Help @winsonlee 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,
)...