title: How to move inline Javascript from HTML to .js files
body: <p>I'm working on creating a basic web based game using the HTML5 canvas and JavaScript. Although I have done some work with the HTML5 canvas before, it hasn't been too extensive and so it hasn't required too much JavaScript- meaning I wrote the JavaScript in script tags in my .html file.</p>
  
  <p>With the file I'm currently working on, I'm now getting to the point where I need to separate the JavaScript from the HTML, since when I view the page in a browser, and click the 'start' button on the canvas, although it does perform the function it's meant to, it takes an age to do it... I assume because of the size of my .html file (most of which is JavaScript).</p>
  
  <p>I have moved all of the JavaScript into separate files, with just one or two functions in each file, but I'm not sure how I then use those functions in my HTML page?</p>
  
  <p>My HTML file currently looks like this:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Home&lt;/title&gt;
  
  &lt;section hidden&gt;
  &lt;img id="StartButton" src="StartButton.png" alt="Start Button" width="179" height="180" href="javascript:drawLevelOneElements();"/&gt;
  &lt;/section&gt;
  
  &lt;script src = "drawLevelOneElements.js" type = "text/javascript"&gt;&lt;/script&gt;
  &lt;script src = "layers&amp;analytics.js" type = "text/javascript"&gt;&lt;/script&gt;
  &lt;script src = "startGameDrawGameElementsDrawStartButton.js" type = "text/javascript"&gt;&lt;/script&gt;
  &lt;script src = "variables&amp;preloadingImages.js" type = "text/javascript"&gt;&lt;/script&gt;
  
  &lt;/head&gt;
  
  &lt;body onLoad="startGame()"&gt;
      &lt;h1&gt;Home&lt;/h1&gt;
      &lt;p1&gt;The purpose of this website is to teach users the basic principles of running a business by playing the game below. &lt;br /&gt;&lt;br /&gt;&lt;/p1&gt;
      &lt;p2&gt;
  
      &lt;canvas id="gameCanvas" width="1000" height="500" style="border:1px solid"&gt;
      Your browser does not support the canvas element.
      &lt;/canvas&gt;
  
      &lt;br /&gt;&lt;br /&gt;&lt;/p2&gt;
      &lt;p3&gt;Use this paragraph to enter text that provides the user with instructions for how to play the game. &lt;br /&gt;
          Update the instructions so that they're appropriate to whatever level the user is currently playing.&lt;/p3&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>The script src lines are referencing the JS files I want to use, but when I view the page in a browser, I just get a blank canvas. Could someone point out to me what I'm doing wrong here? I'm not sure how I 'attach' the JS to the HTML when writing it in a separate file.</p>
  
  <p>Cheers!</p>
  
  <p><strong>Edit 21/11/2012 at 12:00</strong></p>
  
  <p>Having made the changes suggested, I tried viewing the page in Firefox again, and in the Firebug console, when the page loaded, it said that there was an "unterminated regular expression literal in one of my JS files. The file it was complaining about contains this JS code:</p>
  
  <pre><code>    /* Create a canvas layer to display text */
      function displayText(textLayer, message){
          var textLayerContext = textLayer.getContext();
          textLayer.clear();
          textLayerContext.font = "18pt Calibri";
          textLayerContext.fillStyle = "black";
          textLayerContext.fillText(message, 10, 10);
      }
  
      /* Create a canvas layer to display the button */
      window.onload = function(){
          var stage = new Kinetic.Stage({
              container: "container",
              width: 179,
              height: 180
          });
          var buttonLayer = new Kinetic.Layer();
          var textLayer = new Kinetic.Layer();
      }
  &lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;
  /*Google analytics code for tracking website. */
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-31216545-1']);
      _gaq.push(['_trackPageview']);
  
      (function() {
          var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
          ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
          var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
  </code></pre>
  
  <p>and the line it was complaining about was the <code>&lt;/script&gt;</code></p>
  
  <p>It does now work, but making this change doesn't seem to have improved performance much in terms of the time it takes to load an image after clicking the start button... any suggestions?</p>