title: Rendering and executing <script> on pageload after injecting it from external .js file
body: <p>I'm still a beginner with JavaScript and am trying to use it to make the Etsy Mini API responsive. I have two <code>&lt;script&gt;</code>s that need to be added to a <code>&lt;div&gt;</code> in order for the mini shop to appear:</p>
  
  <pre><code>&lt;script type='text/javascript' src='https://www.etsy.com/assets/js/etsy_mini_shop.js'&gt;&lt;/script&gt;
  </code></pre>
  
  <pre><code>&lt;script type='text/javascript'&gt;new Etsy.Mini(&lt;UNIQUE_SHOP_ID&gt;,'thumbnail',4,3,0,'https://www.etsy.com');&lt;/script&gt;
  </code></pre>
  
  <p>In the second <code>&lt;script&gt;</code>, I can specify the size of a thumbnail grid, (4x3 in the example above), but this grid is not responsive, so if it's loaded on a device with a smaller screen, or if a laptop's browser window is resized, the 4x3 grid might be too big. I want to use media queries in an external .js file to change this thumbnail grid, and this is where I'm running into problems.</p>
  
  <p>Below is my current external .js file:</p>
  
  <pre><code>var products = document.getElementById("products-card");
  var grid5x3 = "&lt;script type='text/javascript'&gt;new Etsy.Mini(&lt;UNIQUE_SHOP_ID&gt;,'thumbnail',5,3,0,'https://www.etsy.com');&lt;/script&gt;";
  var grid4x3 = "&lt;script type='text/javascript'&gt;new Etsy.Mini(&lt;UNIQUE_SHOP_ID&gt;,'thumbnail',4,3,0,'https://www.etsy.com');&lt;/script&gt;";
  var grid3x3 = "&lt;script type='text/javascript'&gt;new Etsy.Mini(&lt;UNIQUE_SHOP_ID&gt;,'thumbnail',3,3,0,'https://www.etsy.com');&lt;/script&gt;";
  var grid2x3 = "&lt;script type='text/javascript'&gt;new Etsy.Mini(&lt;UNIQUE_SHOP_ID&gt;,'thumbnail',2,3,0,'https://www.etsy.com');&lt;/script&gt;";
  var grid1x3 = "&lt;script type='text/javascript'&gt;new Etsy.Mini(&lt;UNIQUE_SHOP_ID&gt;,'thumbnail',1,3,0,'https://www.etsy.com');&lt;/script&gt;";
  
  (function () {
      function resize() {
          var min_2500 = window.matchMedia('only screen and (min-width: 2500px)').addListener(function () {
              products.innerText = grid5x3;
          });
          var min_1900_max_2499 = window.matchMedia('only screen and (min-width: 1900px) and ' +
          '(max-width: 2499px)').addListener(function () {
              products.innerText = grid4x3;
          });
          var min_1200_max_1899 = window.matchMedia('only screen and (min-width: 1200px) and ' +
          '(max-width: 1899px)').addListener(function () {
              products.innerText = grid3x3;
          });
          var min_900_max_1199 = window.matchMedia('only screen and (min-width: 900px) and ' +
          '(max-width: 1199px)').addListener(function () {
              products.innerText = grid2x3;
          });
          var max_899 = window.matchMedia('only screen and (max-width: 899px)').addListener(function () {
              products.innerText = grid1x3;
          });
      }
  
      resize();
  }());
  </code></pre>
  
  <p>And here is the .html:</p>
  
  <pre><code>&lt;head&gt;
    ...
    &lt;script type='text/javascript' src='https://www.etsy.com/assets/js/etsy_mini_shop.js'&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    ...
    &lt;div class="products-card" id="products-card"&gt;&lt;/div&gt;
    ...
  &lt;/body&gt;
  </code></pre>
  
  <p>Problem 1: The script isn't executed, but rather the actual text, <code>&lt;script&gt;</code> tags included, appears in the <code>&lt;div&gt;</code>. How can I change my Javascript to make the script execute and render the mini shop?</p>
  
  <p>Problem 2: The <code>&lt;div&gt;</code> is empty upon page load, but after the browser screen is resized and hits one of the cutoffs specified in the media queries, the text appears and changes properly according to the media queries. How can I make the shop render in the <code>&lt;div&gt;</code> on page load and not just when the browser window is resized?</p>
  
  <p><strong>UPDATE 1</strong>
  So I discovered that using innerText/innerHTML does not allow any scripts to be executed/rendered when injected into the html file (<a href="https://stackoverflow.com/questions/1197575/can-scripts-be-inserted-with-innerhtml">based on an answer here</a>). Here is my new .js file:</p>
  
  <pre><code>var products = document.getElementById("products-card");
  var grid5x3 = "new Etsy.Mini(&lt;UNIQUE_SHOP_ID&gt;,'thumbnail',5,3,0,'https://www.etsy.com');";
  var grid4x3 = "new Etsy.Mini(&lt;UNIQUE_SHOP_ID&gt;,'thumbnail',4,3,0,'https://www.etsy.com');";
  var grid3x3 = "new Etsy.Mini(&lt;UNIQUE_SHOP_ID&gt;,'thumbnail',3,3,0,'https://www.etsy.com');";
  var grid2x3 = "new Etsy.Mini(&lt;UNIQUE_SHOP_ID&gt;,'thumbnail',2,3,0,'https://www.etsy.com');";
  var grid1x3 = "new Etsy.Mini(&lt;UNIQUE_SHOP_ID&gt;,'thumbnail',1,3,0,'https://www.etsy.com');";
  
  (function () {
      function resize() {
          var min_2500 = window.matchMedia('only screen and (min-width: 2500px)').addListener(function () {
              var eval_grid5x3 = eval(grid5x3);
              products.innerText = eval_grid5x3;
          });
          var min_1900_max_2499 = window.matchMedia('only screen and (min-width: 1900px) and ' +
          '(max-width: 2499px)').addListener(function () {
              var eval_grid4x3 = eval(grid4x3);
              products.innerText = eval_grid4x3;
          });
          var min_1200_max_1899 = window.matchMedia('only screen and (min-width: 1200px) and ' +
          '(max-width: 1899px)').addListener(function () {
              var eval_grid3x3 = eval(grid3x3);
              products.innerText = eval_grid3x3;
          });
          var min_900_max_1199 = window.matchMedia('only screen and (min-width: 900px) and ' +
          '(max-width: 1199px)').addListener(function () {
              var eval_grid2x3 = eval(grid2x3);
              products.innerText = eval_grid2x3;
          });
          var max_899 = window.matchMedia('only screen and (max-width: 899px)').addListener(function () {
              var eval_grid1x3 = eval(grid1x3);
              products.innerText = eval_grid1x3;
          });
      }
  
      resize();
  }());
  </code></pre>
  
  <p>While this does render the Etsy mini shop, it does so with some odd behaviors:</p>
  
  <ol>
  <li><p>The entire <code>&lt;body&gt;</code> is replaced with the injected script, when I expected the targeted <code>&lt;div&gt;</code> to be replaced with the script.</p></li>
  <li><p>Every time I resize the screen, it <em>adds</em> another Etsy mini shop (it does not replace the one that is currently there), resulting in the page becoming filled with numerous shops.</p></li>
  <li><p>The issue of it not rendering on pageload is still there.</p></li>
  </ol>
  
  <p>Apologies if this is a rather simple question. I've spent a lot of time Googling solutions but nothing I've tried has worked. The code I have above is the closest I've come to having a responsive Etsy Mini shop. Thanks for any answers/comments/suggestions!</p>