title: Ajax wait till redirect to finish executing.
body: <p>I have basically the same problem as the one described in the link below, but I dont find the solution to be very clear. I want my ajax success function to wait until  the window function   is finished executing, THEN modify the divs. Instead, it modifies the divs of the current page, then redirects. <a href="https://stackoverflow.com/questions/22107393/ajax-on-success-redirect-then-modify-new-page">AJAX: on success redirect then modify new page</a></p>
  
  <pre><code>main.js
  
  $("#form").submit( function(e) {
      e.preventDefault();
      var id = $('#searchbar').val(); // store the form's data.   
          $.ajax({
                  url: '/search',
                  type: 'POST',
                  data: {id:id}, 
                  dataType: 'text',
  
  
              success: function(data) {
  
                  //Redirect to the page where we want to display the data
                  window.location.href = '/test';
  
  
                  data = JSON.parse(data);
                  console.log(data);
                  $("#count").text("we analyzed...");
                  $("#result1").text(data.county);
                  $("#totals").text("with a score of..");
                  $("#result2").text(data.totalSentiments);
  
  
     },
     error: function(jqXHR, textStatus, errorThrown){
       console.log("error")
       alert(textStatus, errorThrown);
    }
  });
  
  });
  </code></pre>