title: How can I run a <script> tag that I just inserted dynamically from a BHO
body: <p>I'm completely new to developing IE extensions with Browser Helper Objects. </p>
  
  <p>I managed to create a BHO that successfully inserts a script tag that references a javascript file in the head of the HTML page (see code below). </p>
  
  <p>But the script tag just sits there in the DOM and the external javascript file is <strong>not</strong> executed. </p>
  
  <p>Is there any way to tell the browser to run the external javascript file?</p>
  
  <p>Thanks!</p>
  
  <p><em>Code Details:</em>
  I call the following method on the OnDocumentComplete event:</p>
  
  <pre><code>void CHelloWorldBHO::InsertScriptTag(IDispatch* pDispDoc)
  {
  HRESULT hr = S_OK;
  // query for an HTML document.
  CComQIPtr&lt;IHTMLDocument3&gt; pDocument3 = pDispDoc;
  CComQIPtr&lt;IHTMLDocument2&gt; pDocument2 = pDispDoc;
  if (pDocument2 != NULL &amp;&amp; pDocument3 != NULL)
  {
      // **********************   create our script tag Element  (pHtmlElem) ****************************
      IHTMLElement* pHtmlElem;
      CComVariant vAlert="http://www.gnpcb.org/esv/share/js/?action=getDailyVerse"; // example referencing external JS code
      CComVariant vJavascript="text/javascript";
      hr = pDocument2-&gt;createElement(_T("script"), &amp;pHtmlElem); 
      if (SUCCEEDED(hr) &amp;&amp; pHtmlElem != NULL)
      {
          hr = pHtmlElem-&gt;setAttribute(_T("type"), vJavascript); 
          hr = pHtmlElem-&gt;setAttribute(_T("src"), vAlert);            
      }
  
      // **********************   insert Element  (pHtmlElem) in HTML Head ****************************
      // Get the head from the DOM.
      static const CComBSTR sbstrHead(L"head");
      CComPtr&lt;IHTMLElementCollection&gt; objects;
      hr = pDocument3-&gt;getElementsByTagName(sbstrHead, &amp;objects);
      if(SUCCEEDED(hr) &amp;&amp; objects != NULL)
      {
          // Get the number of elements in the collection.
          long nElements = 0;
          hr = objects-&gt;get_length(&amp;nElements);
          if (hr == S_OK &amp;&amp; nElements &gt; 0)
          {
              CComVariant svarItemIndex(0); // we will get the first element
              CComVariant svarEmpty;
              CComPtr&lt;IDispatch&gt; spdispElement;
  
              // Get the element out of the collection with index 0 (the first element, that is, the head)
              hr = objects-&gt;item(svarItemIndex, svarEmpty, &amp;spdispElement);
              if (hr == S_OK &amp;&amp; spdispElement != NULL)
              {
                  CComQIPtr&lt;IHTMLDOMNode, &amp;IID_IHTMLDOMNode&gt; spHeadNode = spdispElement; // query for DOM interfaces
                  CComQIPtr&lt;IHTMLDOMNode, &amp;IID_IHTMLDOMNode&gt; spNodeNew; 
                  spNodeNew = pHtmlElem; 
  
                  if (spHeadNode)
                  {
                      spHeadNode-&gt;appendChild(spNodeNew, NULL); 
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>}</p>