title: Calling a JS function from a second file using ASP.NET
body: <p>I have an ASP.NET web application that makes use of a large Javascript file to enable front-end functionality. The issue is that since this web application is growing in size the Javascript file is growing in size along with it.</p>
  
  <p>I want to remove some of my larger functions out of my main javascript file <code>site.js</code> and instead contain them inside a second file. My aim is to declutter the main JS file and increase readability etc.</p>
  
  <p>If this were a normal web application I'm sure I'd be able to use JQuery to achieve this through use of the <code>.getScript()</code> however I've tried using this function to pull in a separate script with a simple alert function and I get a reference error saying that my alert function is undefined. I have included both scripts within my ASP.NET <code>_Layout</code> view, but still it doesn't work.</p>
  
  <p>Below is what I am doing currently, what do I need to do to be able to call a JS function held inside another file from site.js?</p>
  
  <p>site.js</p>
  
  <pre><code>$(function() {
  
      $(document).ready(function() {
          $.getScript("../site2.js");
  
          sendAlert();
      });
  
      //... other js
  
  });
  </code></pre>
  
  <p>site2.js</p>
  
  <pre><code>$(function() {
  
      function sendAlert() {
          alert("site2 file");
      }
  
  });
  </code></pre>
  
  <p>_Layout</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;script src="~/js/site2.js"&gt;&lt;/script&gt;
      &lt;script src="~/js/site.js"&gt;&lt;/script&gt;
      &lt;link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt" crossorigin="anonymous"&gt;
      &lt;link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css" integrity="sha384-88btmYK8qOHy4Z2XuhkWZjUOHICKYe1eSDMwaDGOAy802OCu6PD6mwqY5OwnfGwp" crossorigin="anonymous"&gt;
      &lt;script defer src="https://use.fontawesome.com/releases/v5.1.0/js/all.js" integrity="sha384-3LK/3kTpDE/Pkp8gTNp2gR/2gOiwQ6QaO7Td0zV76UFJVhqLl4Vl3KL1We6q6wR9" crossorigin="anonymous"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  @RenderBody()
  
  
  @RenderSection("Scripts", required: false)
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>