0

I have this link:

$('.popup-window').click(function (e) {
    e.preventDefault();

    $.ajax({
        ...
    })
});

which is a .NET LinkButton (a link that call a javascript, not a real href). I want to prevent Default if ajax return some (let say, false). Else, if true, continue with that link handler.

How can I do it?

P.S. I need that e.preventDefault(); at the beginning, else .NET handler will act immediatly.

markzzz
  • 47,390
  • 120
  • 299
  • 507

3 Answers3

1

I think I understand what you're looking for.

Here's my idea on it: use a data attribute on the DOM element to decide weither default event should be prevented or not. Initially, the event is prevented but the ajax has the power to "unlock?" it, then fire it again. It's a little bit of custom work but it may do the trick:

var $popupWindow=$('popup-window');

// initially "tell" the LinkButton to prevent default
$popupWindow.data('prevent-default', 1);

// the click event (initially prevents default)
$popupWindow.click(function(e){
    var $this=$(this);

    if ($this.data('prevent-default')==0) { // if "unlocked"

        // "lock" it again (default isn't prevented)
        $this.data('prevent-default', 1);

    } else { // if "locked"
        // default is prevented
        e.preventDefault();

        // test if it should be unlocked
        $.ajax({
            // ...
        }).done(function(data){
            if (data.length>0 && data.response==false) {
                // set the attribute so it shouldn't prevent default
                $this.data('prevent-default', 0);

                // trigger this click (should let the event fire completely)
                $this.trigger('click');
            }
        });
    }
});

UPDATE:

An alternative could be to add a Page Method.

(See: Using jQuery to directly call ASP.NET AJAX page methods)

This would reduce the mechanics to somethink like this:

$('popup-window').click(function(e){
    e.preventDefault();

    $.ajax({
        // ...
    }).done(function(data){
        if (data.length>0 && data.response==false) {
            $.ajax({
                type: "POST",
                url: "YourPage.aspx/YourMethod",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    // Replace the div's content with the page method's return.
                    $("#Result").text(msg.d);
                }
            });
         }
     });
});
Frederik.L
  • 5,522
  • 2
  • 29
  • 41
  • It doesn't work! When I `$this.trigger('click');`, next I catch the `// "lock" it again (default isn't prevented)` statement, but the .NET linkbutton it is not executed. – markzzz Sep 20 '13 at 09:42
  • I guess that jquery won't do the postback after all... but you can use a Page Method and call it through ajax. Look here: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/ – Frederik.L Sep 20 '13 at 09:51
1

You can use the __doPostBack() js function to trigger the postback in your AJAX callback.

The only thing is that you need to pass in the id of the control causing the postback, e.g.

 __doPostBack('btnPopup', null);

you can see more on this function in this question: How to use __doPostBack()

Community
  • 1
  • 1
RobH
  • 3,604
  • 1
  • 23
  • 46
0
 $('.popup-window').click(function (e) {
data = '?sample=1' //serialized data here
callback    = function(json){
    if(json.returnVar!=true){ //check if returnVar is not true
        e.preventDefault();   //prevent redirect if not true
    }
};

$.ajax({
      type: 'POST',
      url: "../ajaxcall.php", //the url to call for ajax here
      data: data,
      success: callback,
      dataType: 'json'
    });
});

Try this, let me know if you can't understand the code

Dee Jay
  • 141
  • 10
  • No. I need to put the prevent default at the start of the code, otherwise .NET framework acts immediatly. – markzzz Sep 20 '13 at 08:49