2

I am new to jquery and encountered an issue regarding the loader to display while browser is loading. In my MVC application, I have ajax calls and on success(in some calls) i need to redirect to a url(got from response), I have implemented ajax.start and ajax.complete and is working fine for ajax call but when the window.location.href is called on success the loader hides and the browser starts loading, though i have also written code on window.onbeforeunload(). I need to have loader on ajax call as well as browser loading . Please help me out.

$(document).ajaxStart(function () {       
$("#loaderDiv").show();   
});

 $(document).ajaxComplete(function () {    
 $("#loaderDiv").hide();   
 });  

  window.addEventListener('beforeunload', function (e) {   
 $("#loaderDiv").show();   
 });

And the ajax call is :

  success: function (result) {                
            if (result.Success == true && result.url != null && result.url 
              != undefined && result.url != "")
            {
                window.location.href = result.url;   
            }
2
  • I need to have loader on ajax call as well as browser loading Commented Dec 11, 2019 at 9:33
  • Just add $("#loaderDiv").show(); directly in front of location.href = and move on. Doesn't need to be complicated/automatic. The issue is that as soon as your code hits location.href all javascript stops so ajaxComplete never fires. Commented Dec 11, 2019 at 9:59

1 Answer 1

1

Did you try this?

$.ajax({
        //some condition
        },
        beforeSend: function (){
          //your loader code
        },
    });
Sign up to request clarification or add additional context in comments.

1 Comment

thanx, but the loader works fine with ajax call the problem is when on success of ajax call as soon as it hits the window.href it closes the loader and start page loading in browser but i need the loader to be remain display on browser loading also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.