title: How to remove a script tag reliably before it is loaded?
body: <p>I've created a mutation observer to remove (block) scripts under certain conditions.</p>
  <p>There are several SO solutions which suggest it can work. Eg: <a href="https://stackoverflow.com/a/65453574/4688612">https://stackoverflow.com/a/65453574/4688612</a></p>
  <p>But it doesn't work in my case for <code>&lt;script src=&quot;https://www.googletagmanager.com/gtag/js?id=UA-12345678-9&quot;&gt;&lt;/script&gt;</code></p>
  <p>For some reason the network call to googletagmanager.com still happens on every page load.</p>
  <p>Google Chrome and Firefox both load the script.</p>
  <p>Now I'm not sure if using the mutation observer method is simply not reliable or buggy, or if it is a bug in my code.</p>
  <p>Is there a reliable solution for this?</p>
  <p>Here's my code.</p>
  <pre class="lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
      
  &lt;head&gt;
      &lt;meta charset=&quot;UTF-8&quot;&gt;
      &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
      &lt;title&gt;Document&lt;/title&gt;
      
      &lt;link rel=&quot;shortcut icon&quot; href=&quot;#&quot;&gt;
      
      &lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;
              integrity=&quot;sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=&quot; crossorigin=&quot;anonymous&quot;&gt;
              &lt;/script&gt;
      
      &lt;script&gt;
      
          const observer = new MutationObserver( (mutations) =&gt;{
                  mutations.forEach(({addedNodes}) =&gt; {
                      [...addedNodes]
                          .forEach(node =&gt; {
                              $(node).remove()
                          });
                  });
              })
  
          observer.observe(document.head, { childList: true });
     
      &lt;/script&gt;
      
      &lt;script src=&quot;https://www.googletagmanager.com/gtag/js?id=UA-12345678-9&quot;&gt;&lt;/script&gt;
          
  &lt;/head&gt;
      
  &lt;body&gt;&lt;/body&gt;    
  &lt;/html&gt;
      ```
  </code></pre>