dcolumbus
dcolumbus

Reputation: 9722

jQuery Exit Popup Redirect?

So I've been looking around for hours, testing multiple versions, testing some of my own theories and I just can't seem to get it working.

What I'm trying to do is use alert or confirm (or whatever works) so popup a dialog when a user tries to navigate away from a purchase form. I just want to ask them "Hey, instead of leaving, why not get a free consultation?" and redirect the user to the "Free Consultation" form.

This is what I have so far and I'm just not getting the right results.

$(window).bind('beforeunload', function(){
        var pop = confirm('Are you sure you want to leave? Why not get a FREE consultation?');
        if (pop) {

            window.location.href('http://www.mydomain/free-consultation/');
        } else {
            // bye bye
        }
    });

    $("form").submit(function() {
        $(window).unbind("beforeunload");
    });

Upvotes: 1

Views: 4728

Answers (4)

dcolumbus
dcolumbus

Reputation: 9722

Instead of using the beforeunload and alert(), I decided to check whether or not the users mouse has left the document. See code below:

$(document).bind('mouseleave', function(event) {
    // show an unobtrusive modal
});

Upvotes: 1

Vinod
Vinod

Reputation: 4872

Try this:

window.onunload = redirurl;
            function redirurl() {
                alert('Check this Page');
                window.location.href('http://www.google.com');
            }

Upvotes: 0

asitmoharna
asitmoharna

Reputation: 1532

Not sure whether it will help. You need to stop the propagation before showing the Confirm / Alert.

Please refer http://jonathonhill.net/2011-03-04/catching-the-javascript-beforeunload-event-the-cross-browser-way/

Look at the last comment.

Upvotes: 0

arunes
arunes

Reputation: 3524

This is showing confirm dialog to user, want to stay or leave page. Not exactly what you looking for but maybe it will be useful for start.

function setDirtyFlag() {
    needToConfirm = true; //Call this function if some changes is made to the web page and requires an alert
    // Of-course you could call this is Keypress event of a text box or so...
}

function releaseDirtyFlag() {
    needToConfirm = false; //Call this function if dosent requires an alert.
    //this could be called when save button is clicked 
}


window.onbeforeunload = confirmExit;
function confirmExit() {
    if (needToConfirm)
        return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
}

Script taken from http://forums.devarticles.com/showpost.php?p=156884&postcount=18

Upvotes: 1

Related Questions