By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,481 Members | 1,713 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,481 IT Pros & Developers. It's quick & easy.

opera: how to ask user before leaving page

P: n/a
we'd like to make smth like google implemented in gmail: when user
compose mail and then wants to leave page without actually sending it,
gmail notifies user about mail was not sent. this should happens when
user clicks link or press browser back button.
So the problem is that Opera does not support "beforeunload" event and
thus it is not possible to inform user that hes about to lose his
work. is there a way another then adding scripts to all the link on
the page?
Jul 30 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Jul 30, 11:11*am, koruyucu <i.sinis...@gmail.comwrote:
we'd like to make smth like google implemented in gmail: when user
compose mail and then wants to leave page without actually sending it,
gmail notifies user about mail was not sent. this should happens when
user clicks link or press browser back button.
So the problem is that Opera does not support "beforeunload" event and
thus it is not possible to inform user that hes about to lose his
work. is there a way another then adding scripts to all the link on
the page?
In opera I don't think there's anyway to intercept when a new url is
typed in the address bar or if the window is closed. Gmail also does
not catch these events in Opera, so it's not like they use some
special magic there. They only prompt you if you've clicked on a
hyperlink within their page. That you can catch.

As far as clicking links on the page, this you can have some control
over. When a user "composes an email" you could turn some variable
flag on (i.e. set to true), and then add an onclick handler to all
your hyperlinks which would check that flag. If that flag is set,
supply a confirm window (i.e. "You have unsent mail, do you wish to
continue?") and have the handler return their choice. If they select
cancel(false) return false which will prevent the navigation. if they
select Ok(true) return true which will follow the navigation.

Ask anyone here and they'll tell you I'm no expert...but an example of
how this could be accomplished is:

<script type="text/javascript">
var inProgress = false;

function lockProgress() {
inProgress = true;
}

function checkLink() {
if (inProgress) {
return confirm("You have unsent mail. Do you wish to
continue?");
}
else {
return true;
}
}

function addLinkCheck() {
for (var i = 0; i < document.getElementsByTagName("A").length; i+
+) {
document.getElementsByTagName("A")[i].onclick = checkLink;
}
}
</script>

...

<body onload="addLinkCheck();">
...

What will happen here is that when the page is loaded, addLinkCheck is
called which will assign the checkLink function as the onclick handler
for all your anchor tags. If inProgress has been turned on (this is
your part as I know nothing about your page) then a confirmation
window will be presented before the navigation is followed. If they
click Ok, the navigation will go as usual, if they click Cancel, the
navigation will be cancelled. At least that's what happened on my test
page :).

You'll have to remember to set the inProgress variable back to false
once you have a situation where you don't want links blocked (i.e.
they send the mail or cancel out of the mail.).

HTH.
Jul 30 '08 #2

P: n/a
function addLinkCheck() {
for (var i = 0; i < document.getElementsByTagName("A").length; i+
+) {
document.getElementsByTagName("A")[i].onclick = checkLink;
}}
perfect.

thank you :)

Thiago
Jul 30 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.