Here's my Html document
<html> <body> Something <br> <div id="forscript">some more text</div> </body> </html>
which I pass as the Script argument to AddScript() below.
This is my code (doc2 is an IHtmlDocument2 obtained from the WebBrowser):
procedure TForm1.AddScript(const Script : String); var Element : IHtmlElement; Doc3 : IHtmlDocument3; begin Doc2.QueryInterface(IHtmlDocument3, Doc3); Element := Doc3.GetElementByID('forscript'); Element.innerHTML := Element.innerHTML + Script; end;
This works fine, but ...
The reason for the Element.innerHTML on the RHS of the assignment is simply that I found that if the RHS contains only the Script js, the js does't execute. My question is, why not, and is there a simpler alternative, like somehow creating an IHtmlScriptElement in code and inserting it in the DOM? Obviously my simple-minded work-around is simply to prepend the text the Element already contains, but I have a slightly hard time believing that people who actually know what they're doing would find that necessary.
FWIW #1: I tried using Element.insertAdjacentHtml to add the script but got the same behaviour as I've just described, in terms of needing to insert something in addition to the script to get the script to be executed, so I'm wondering whether it's something to do with how the DOM is processed after a change is made to it.
FWIW #2: I've used the Element.innerHtml route because TWebBrowser/MSHTML.Pas have resisted my attempts to create an IHtmlScriptElement in code; AFAICS, attempting to use any of the CohtmlXXXElement routines in MSHTML.Pas provoke a "Class not registered" exception, which seems to confirm a comment I came across somewhere by @kobik of this parish.
(Btw, using D7 + IE11 on Win7 64-bit)
Here' a complete example how to use
Html is loaded at application startup.
The code under
©2020 All rights reserved.