title: Remove a script using Id
body: <p>I have the below script which I am embedding and removing to embed the next one. however I want remove the previous tag by id and not head.childNodes[head.childNodes.length - 1]. Can anyone pls help.</p>
  
  <pre><code>var head = document.getElementsByTagName('head')[0];
      var tag = document.createElement('script');
      tag.type = 'text/javascript';
      tag.id = 'adobedatalayer'
      tag.textContent = "var DDO = {}; DDO.pageData = {'pageName': '" + pageLoadData.pageUrl + "'} ";
      if (this._dataLayerInjected) {
        var script = head.childNodes[head.childNodes.length - 1]; //get previous script element
        head.removeChild(script); //removing script
        head.appendChild(tag); // appending the script again
      } else {
        head.appendChild(tag);
        this._dataLayerInjected = true;
      }
  </code></pre>