title: Jquery Ajax call to set variables doesn't persist outside of call
body: <p>Not entirely sure why this is going wrong. Jquery Novice. It should work, according to all the examples I've seen, but it doesn't.</p>
  
  <p>In the following script, the function sets new values for variables, but the new values don't persist outside the function. (console logs show this)</p>
  
  <pre><code>$(document).ready(function() {
  
      // url
      var path = location.pathname.split("/"),
          pos1_path = path[1] != undefined ? path[1] : false,
          pos2_path = path[2] != undefined ? path[2] : false,
          pos3_path = path[3] != undefined ? path[3] :  false;
  
      //console.log('before: ' + pos2_path);
      if (pos1_path = 'article') {
          $.post("/js/ajax/article_crumbs.php", { func: "getArticleCrumbs" },
              function(data){
                  pos2_path = data.category;
                  //console.log('during: ' + pos2_path);
              return pos2_path}, "json");
      }
      //console.log('after: ' + pos2_path);
  
      // navigation
      // set category nav to current category
      if ( path[2] ) {
          $('#category_nav a[class$="' + pos2_path + '"]').toggleClass('current');
      }
      else {
          $('#category_nav a:first').toggleClass('current');
      }
  });
  </code></pre>
  
  <p>The PHP:</p>
  
  <pre><code>&lt;?php echo json_encode(["category"=&gt;"arts-and-culture"]);
  </code></pre>
  
  <p>Also, according to the console printouts, the "after" print out appears before the "during", does that mean the function doesn't run until the end of the rest of the script?</p>
  
  <p><strong>EDIT:</strong> To clarify, I need to keep the <strong>{ if(path[1]) <em>setcurrent</em>  }</strong> part of the script out of the <strong>{ if(pos1_path = 'article') }</strong> part so that if pos1_path is NOT article, it sets the value to whatever is in the URI..</p>
  
  <p><strong>SOLUTION</strong> as per answer below:</p>
  
  <pre><code>$(document).ready(function() {
  
      // url
      var path = location.pathname.split("/"),
          pos1_path = path[1] != undefined ? path[1] : false,
          pos2_path = path[2] != undefined ? path[2] : false,
          pos3_path = path[3] != undefined ? path[3] :  false;
  
      if (pos1_path = 'article') {
          //console.log('before: ' + pos2_path);
          var data = {id: pos2_path}
          $.ajax({
              type: 'POST',
              url: "/js/ajax/article_crumbs.php",
              data: data,
              success: function(data){
                  pos2_path = data.category;
                  //console.log('during: ' + pos2_path);},
              dataType: "json",
              async: false
          });
      }
      //console.log('after: ' + pos2_path);
  
      // navigation
      // set category nav to current category
      if ( path[2] ) {
          $('#category_nav a[class$="' + pos2_path + '"]').toggleClass('current');
      }
      else {
          $('#category_nav a:first').toggleClass('current');
      }
  }
  </code></pre>