0

A visitor wants to leave the page (reload, refresh, close tab..etc.), and then a function is triggered prompting a modal "are you sure you want to leave?".

I want to allow them to leave the page without the modal prompt if they click on a link.

Here is a jsfiddle example- http://jsfiddle.net/vvj90z8h/3/

<a href="http://www.google.com" class="button">Proceed</a>      
<div class="price">$139.99</div>

// function
priceFunction = function (){
    window.onbeforeunload = function() {
        return 'Sure you want to leave?';
    }
};

// if price is 139.99, run function
var price = $(".price").text().replace(/[^0-9\.]/g, '');
if (price = 139.99){
    priceFunction();
}

If you refresh the page, the modal shows, which is okay.

I'm wanting to click the link and not have the modal show.

LondonRob
  • 73,083
  • 37
  • 144
  • 201
Bjorn.B
  • 1,473
  • 1
  • 10
  • 11
  • on what basis you want it to not prompt?? – Ehsan Sajjad Sep 09 '14 at 16:50
  • Maybe it would be easier to have "not prompting" be the default action and "prompting" be the explicit action? – David Sep 09 '14 at 16:50
  • This is an exact duplicate of [this](http://stackoverflow.com/questions/18783535/jquery-beforeunload-when-closing-not-leaving-the-page) question. – LondonRob Sep 09 '14 at 16:54
  • @EhsanSajjad the basis for it not to show was if that link was clicked – Bjorn.B Sep 09 '14 at 17:18
  • @David I had tried that route but the first condition was if the url contained "shoppingcart". I think that threw me off so I missed your suggested route in the first place. Thanks for the suggestion though, I might just try that anyway :) – Bjorn.B Sep 09 '14 at 17:21

3 Answers3

1

You can unbind the onbeforeunload event listener on click :

$('#proceed').click(function(){ window.onbeforeunload = null; });

Demonstration

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

Set a flag when they click or unbind the event

(function(){
    var pass = false;
    $(document.body).on("mousedown","a.button", function () {
        pass = true;
    });

    window.onbeforeunload = function() {
        if(pass) return;
        return 'Sure you want to leave?';
    }

}());

or

(function(){

    $(document.body).on("mousedown","a.button", function () {
        $(window).off("beforeunload");    
    });

    $(window).on("beforeunload", function() {
        return 'Sure you want to leave?';
    });

}());
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

http://jsfiddle.net/jcqjj1u3/2/

// function
priceFunction = function (){
    window.onbeforeunload = function() {
            return 'Sure you want to leave?';
    }
};

// if price is 139.99, run function
var price = $(".price").text().replace(/[^0-9\.]/g, '');
if (price = 139.99){
    priceFunction();
}

var beforeUnloadFunction;

$('.button').click(function(){
    beforeUnloadFunction = window.onbeforeunload; // save the function for later just in case
    window.onbeforeunload = null;
    // following code is optional. Use it only if you need to do other stuff befre leaving
    event.preventDefault();
    // do other stuff before leaving if you'd like to
    location.href= $(this).attr('href');
});
svassr
  • 5,538
  • 5
  • 44
  • 63