title: How can I overwrite the btnSubmit_onclick javascript function on a web page using the WPF WebBrowser?
body: <p>I'm working with the WPF <code>WebBrowser</code> control which is different from the WinForms version and many recommendations from this excellent web site simply do not work in WPF.</p>
  
  <p>How can I overwrite the <code>btnSubmit_onclick()</code> javascript function on a web page?</p>
  
  <p>This function generates a confirm dialog (like "Are you sure?") which I want to bypass. Basically I want to inject a new function <code>btnSubmit_onclick()</code> which will replace the existing one and I can omit annoying confirm dialog. I have access to MSHTML and all page objects, but struggle to inject script at the header of the page. So far I've got: </p>
  
  <pre><code>mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)webBrowser.Document;
  mshtml.IHTMLElementCollection heads = doc.all.tags("head") as mshtml.IHTMLElementCollection;
  mshtml.IHTMLElement head = heads.item(null, 0) as mshtml.IHTMLElement;
  mshtml.IHTMLElement se = doc.createElement("script") as mshtml.IHTMLElement;
  se.innerHTML = "function btnSubmit_onclick() { alert('here we are'); }";
  </code></pre>
  
  <p>But now I need to inject this new script element into head object before other the other scripts, and I hope it will owerwrite existing <code>btnSubmit_onclick()</code> function downstream.</p>
  
  <p>I unsuccessfully tried head.injectAdjusentHTML which refuses to inject any script into the header even I use DEFER property.</p>
  
  <p>How can i do it? </p>