When a user leaves one page of my website, there should be a warning message which gives the user the option to stay on the page:
"Are you sure that you want to close this page?"
It doesn't matter if the next page is an internal or external page.
I thought this could be done with the onUnload event handler, couldn't it?
<body onunload="confirmClose()">
The confirmClose() function must then show a message box with two buttons so that the user can choose between "Really leave" or "Stay".
function confirmClose() {
return confirm('Really leave this page?')
}
But this function can't stop the unload, right?
Do you have a solution for my problem? Thanks in advance!
When a user leaves one page of my website, there should be a warning message which gives the user the option to stay on the page:
"Are you sure that you want to close this page?"
It doesn't matter if the next page is an internal or external page.
I thought this could be done with the onUnload event handler, couldn't it?
<body onunload="confirmClose()">
The confirmClose() function must then show a message box with two buttons so that the user can choose between "Really leave" or "Stay".
function confirmClose() {
return confirm('Really leave this page?')
}
But this function can't stop the unload, right?
Do you have a solution for my problem? Thanks in advance!
Share Improve this question asked Dec 28, 2009 at 17:47 cawcaw 31.5k65 gold badges184 silver badges293 bronze badges 3- 6 I have a solution to your problem: don't ask the user to confirm leaving your site. It's nothing but annoying. – user21926 Commented Dec 28, 2009 at 17:50
- 6 There are legitimate uses for this, for example when there is user input that would be lost when leaving the page. – Pekka Commented Dec 28, 2009 at 17:51
- I must agree with both of you. It is really annoying but can be useful, though. I want to use it in an adequate case where it prevents the user from losing data. – caw Commented Dec 28, 2009 at 21:24
3 Answers
Reset to default 5You can only provide the text. The browser handles the dialog box (security reasons). Here is some code you can use:
window.onbeforeunload = function (e) {
var e = e || window.event;
var msg = 'Are you sure you want to leave?';
// For IE and Firefox
if (e) {
e.returnValue = msg;
}
// For Safari / chrome
return msg;
}
Try this out on different browsers, though. You'll notice the message should be constructed differently depending on the different browsers' dialog wording.
Here is what I use:
if (IsChrome) {
return 'You were in the middle of editing. You will lose your changes if you leave this page';
} else {
return 'You were in the middle of editing. Press OK to leave this page (you will lose your changes).';
}
You can add an onbeforeunload
event. Note that it can only return a text string to include in the dialog the browser will display when the event is called. You can't tweak the dialog beyond that, nor can you trigger your own confirm
as you're trying to do.
I'd note that this is very, very annoying behaviour except in a few specific situations. Unless you're saving me from significant data loss with this, please don't do it.
The browser takes care of displaying the confirm window.
You need to return the string with the message that you want to ask. Also, you may want to use onbeforeunload
for cross browser patibility.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744312474a4568016.html
评论列表(0条)