title: How to conditionally apply different backgrounds to a canvas
body: <p>The code shown here may be useful to others: It shows the solution I came up with to a requirement I had to, conditionally, apply different background images to a canvas. Questions on how to achieve this or variations of this have been asked here before.</p>
  
  <p>(Sorry for my botched editing. I had originally posted this as a question, then, while reading my posted code, the answer dawned on me, so I removed that part of the post. I didn't realise at the time that there were already two answers, which would make no sense to anyone who didn't see the original question. So, to summarize it: I couldn't work out why functions I had added to someone else's published code that I was adapting couldn't be accessed by my function calls. I didn't realise that the code I was inserting them into was "wrapped" inside an anonymous IFFE function. When I moved them below the end of the "wrapper", I was able to call them from outside the IFFE.)</p>
  
  <p>Thanks javinor, for the link to that very informative article explaining the IFFE function.</p>
  
  <p>As for the method shown below, I have since found a better way to do it. I learned that div elements do not really belong in the HTML head section, so this is a better way:</p>
  
  <pre><code>var lnk;
  lnk = document.createElement('LINK');
  lnk.rel  = 'stylesheet';
  lnk.type = 'text/css';
  lnk.href = 'CanvasStylesA.css';
  document.getElementsByTagName('head')[0].appendChild(lnk);
  </code></pre>
  
  <p>You can wrap a condition around the href line that decides which file to use.</p>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>&lt;head&gt;
  &lt;div id="conditionalCSSincludes"&gt;
  
    &lt;!--
      The javascript below will insert an inner div section here called "CanvasStyles", which
      will, conditionally, contain one of the following two lines. That line will link to one of
      two external style sheets containing the canvas style for the canvas displayed by this page.
  
      &lt;link rel="stylesheet" type="text/css" href="CanvasStylesA.css"&gt;
      &lt;link rel="stylesheet" type="text/css" href="CanvasStylesB.css"&gt;
  
      The two style sheets differ with respect to the background image set for Canvas1.
      A sets it to A.jpg, B sets it to B.jpg.
    --&gt;
  
  &lt;/div&gt;
  &lt;script src="code.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;!-- the javascript below goes here --&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
   &lt;canvas width="530" height="530" id="Canvas1"&gt;
   &lt;p&gt;This page will not display correctly because your browser does not support the canvas element. Sorry.&lt;/p&gt;
   &lt;/canvas&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p><strong>Javascript</strong></p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
    var includesDiv1, includesDiv2, includesDiv3, dt, hour, Bgnd;
  
    // Conditional code that, depending on page load time, chooses
    // whether to display the night-time or daytime background
  
    dt = new Date();
    hour = dt.getHours();
  
    // for testing, enable the line below and set the hour manually ...
    // hour = 6;
  
    if (hour &lt; 6 || hour &gt;= 18){Bgnd = 1;} else{Bgnd = 2;}
  
    // These are the two versions of the include link to the external style sheets:
    includesDiv1 = '&lt;div id="CanvasStyles"&gt; &lt;link rel="stylesheet" type="text/css" href="CanvasStylesA.css"&gt; &lt;/div&gt;';
    includesDiv2 = '&lt;div id="CanvasStyles"&gt; &lt;link rel="stylesheet" type="text/css" href="CanvasStylesB.css"&gt; &lt;/div&gt;';
  
    if (Bgnd==1){
       includesDiv3 = includesDiv1;  // use the night-time background image
    }
    else{
       includesDiv3 = includesDiv2;  // use the daytime background image
    }
  
    // Insert into the conditionalCSSincludes div above an inner div called "canvasStyles".
    // It contains an html include link to an external css file containg canvas styles.
  
    cssIncludesDiv = document.getElementById("conditionalCSSincludes");
    cssIncludesDiv.innerHTML = includesDiv3;
  </code></pre>