0

define two functions like this:

    <script>
        $(function () {
            function testb() {
                alert("ddd");
            }
        })

        function testa() {
            alert("ddd");
        }
    </script>

when page loaded show an ajax page, then call thoese functions from an ajax page, testa will be successful called but not testb.

Bucketcode
  • 461
  • 2
  • 13

1 Answers1

0

If your function is inside the document.ready then you will be able to call global functions as well in ajax success. if your ajax call is in global. then you will not be able to call a function inside document.ready. because of scope limitations..

samples: Ajax inside document.ready

 function testb() {
    alert('ddd');
}
$(function () {

    $('[id$=btnsubmit]').on('click', function () {

        $.ajax({
            type: "GET",
            dataType: "json",
            url: "/Content/test",
            success: function (data) {
                // alert(data);
                testa();
                testb();
            }
        });

    });
    function testa() {
        alert('zxfgsfg');
    }
});

it will work..

IF Still you want to call that function in that way, please check below solution:

    function tesss() {
    alert('ddd');
}

var M7 = {};

$('[id$=btnsubmit]').on('click', function () {
    $.ajax({
        type: "GET",
        dataType: "json",
        url: "/Content/test",
        success: function (data) {
            // alert(data);
            M7();
            tesss();
        }
    });

});
$(function () {
    M7 = function () {
        alert('zxfgsfg');
    };
});
Ram Singh
  • 6,664
  • 35
  • 100
  • 166
  • in my case, i don't need call any function after ajax success. what i want to do is use ajax load a html page, then use onclick inside this page to call a function, i just don't know why it's work only when function is alone. – Bucketcode Dec 07 '16 at 13:43