opera: how to ask user before leaving page

K

koruyucu

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?
 
T

Tom Cole

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").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.
 
T

Thiago Macedo

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


perfect.

thank you :)

Thiago
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top