opera: how to ask user before leaving page

Discussion in 'Javascript' started by koruyucu, Jul 30, 2008.

  1. koruyucu

    koruyucu Guest

    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?
    koruyucu, Jul 30, 2008
    #1
    1. Advertising

  2. koruyucu

    Tom Cole Guest

    On Jul 30, 11:11 am, koruyucu <> wrote:
    > 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.
    Tom Cole, Jul 30, 2008
    #2
    1. Advertising


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


    perfect.

    thank you :)

    Thiago
    Thiago Macedo, Jul 30, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    25,911
    rohit.net100
    Feb 28, 2011
  2. Arnold Peters
    Replies:
    1
    Views:
    1,529
    Tomer Ben-David
    Oct 27, 2004
  3. Chris Roden
    Replies:
    4
    Views:
    573
    Mark Rae [MVP]
    Dec 13, 2007
  4. daveh551

    Detecting user leaving page

    daveh551, Aug 5, 2008, in forum: ASP .Net
    Replies:
    4
    Views:
    498
    Scott M.
    Aug 6, 2008
  5. Jamie Jackson

    Warn to Save before leaving a page...

    Jamie Jackson, Oct 9, 2003, in forum: Javascript
    Replies:
    5
    Views:
    151
    Jamie Jackson
    Oct 10, 2003
Loading...

Share This Page