0

I have the following actions in my document.ready. What I'm trying to do is to selectively remove a div with ID that ends with '_View_memberDirectory'. Therefore, I'm adding the following code which works if it's not part of my function below. However, when I have it inside my function it does not work. At the moment the boolean is always true. Any ideas?

$("[id$='_View_memberDirectory']").bind('DOMNodeInserted', function (event) {
      $(this).empty();
});

My script:

<script type="text/javascript">
    $(document).ready(function () {

        $(function () {
            var bIsContact;
            bIsContact = IsContact();
            if (bIsContact.responseText === "true") {
                     //does not work here
                $("[id$='_View_memberDirectory']").bind('DOMNodeInserted', function (event) {
                    $(this).empty();
                });

            }
            else {
                e.preventDefault;
            }
        });

      //works here <-------
    });

Edit: Added IsContact() function

function IsContact() {
        var friendId =  <%= Request.QueryString("UserID").ToString()%> + '';
        var sf = $.ServicesFramework(<%=ModuleId %>);
        var serviceUrl = sf.getServiceRoot('OmniBody');
        var obj = { "friendId": friendId };

        return $.ajax({
            type: "POST",
            cache: false,
            url: serviceUrl + "/ModuleTask/IsContact",
            beforeSend: sf.setModuleHeaders,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(obj)
        }).done(function (result) {

        }).fail(function (xhr, result, status) {
            alert(result);
        });
    }
alwaysVBNET
  • 3,150
  • 8
  • 32
  • 65

1 Answers1

0

it is not recommended to use async: false in AJAX call

instead, you can try like this

$(document).ready(function() {

    $(function() {
        var bIsContact;
        IsContact().done(function(result) {
            if (result) { // assuming result is boolean value

                // do your stuff here
                $("[id$='_View_memberDirectory']").bind('DOMNodeInserted', function(event) {
                    $(this).empty();
                });

            } else {
                //something elase
            }
        }).fail(function(xhr, result, status) {
            alert(result);
        });
    });

    //works here <-------
});

function IsContact() {
    var friendId = <%= Request.QueryString("UserID").ToString()%> + '';
    var sf = $.ServicesFramework(<%=ModuleId %>);
    var serviceUrl = sf.getServiceRoot('OmniBody');
    var obj = {
        "friendId": friendId
    };
    return $.ajax({
        type: "POST",
        cache: false,
        url: serviceUrl + "/ModuleTask/IsContact",
        beforeSend: sf.setModuleHeaders,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(obj)
    });
}
Ja9ad335h
  • 4,995
  • 2
  • 21
  • 29