title: Use local javascript variable in ajax url
body: <p>I have a local variable within a function called by a click event. The variable is different depending on which button is clicked.</p>
  
  <p>I'm then trying to pass that variable to another function which contains AJAX.</p>
  
  <p>I can't seem to use the first local variable in the ajax function.</p>
  
  <p>I've tried using "window.myVariable" to make it global but it doesn't work. As you can see in the code I've also added call to the next function with my variable as a parameter but no dice. The error I'm getting in the console is "variable 'user' is not defined." </p>
  
  <p>I cannot access the local variable outside of it's original function at all. Can anyone see why it is refusing to become a global variable?.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function getStateOfChat(){
   if(!instanse){
     instanse = true;
     $.ajax({
        type: "POST",
                             url: user + "/process.php",
        data: {  
           'function': 'getState',
           'nickname': nickname,
        'file': file
        },
        dataType: "json",
     
        success: function(data){
         state = data.state;
         instanse = false;
        },
     });
   }  
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>var user1 = "john";
  var user2 = "andrew";
  
  // ============== USER 1 ==============
      
  
          &lt;button type="button" class="btn btn-info btn-lg chatBtnMain" data-toggle="modal" data-target="#myModal2" id="chatBtn1"&gt;
                  Chat to 
                  &lt;span class='nickname'&gt;
                      John
                  &lt;/span&gt;
          &lt;/button&gt;
  &lt;script type="text/javascript"&gt;
     document.getElementById('chatBtn1').addEventListener('click', runScript1);
      function runScript1() {
          var user = user1;
          getStateOfChat(user);
      }
  &lt;/script&gt;
       
      
      
      // ============== USER 2 ==============
          
          &lt;button type="button" class="btn btn-info btn-lg chatBtnMain" data-toggle="modal" data-target="#myModal3" id="chatBtn2"&gt;
                  Chat to 
                  &lt;span class='nickname'&gt;
                      Andrew
                  &lt;/span&gt;
          &lt;/button&gt;
  &lt;script type="text/javascript"&gt;
     document.getElementById('chatBtn2').addEventListener('click', runScript2);
     
      function runScript2() {
          var user = user2;
          getStateOfChat(user);
      }
  &lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>