views:

395

answers:

2

Hi all,

How do I create a floating DIV in a web page on load using BHO with C#.NET?

I just want to inject an HTML code snippet for DIV that will display "Hello World" on the page. The div should have absolute position styles.

-Datte

A: 

Just sink the documentcomplete event, get the parent window of the completed document and call ExecScript on the window object. For javascript code that inject HTML to document, check the DHTML reference in IE SDK or find a open source DHTML class library like jquery and read its code.

Sheng Jiang 蒋晟
A: 

I use:

HTMLDocument document = (HTMLDocument)webBrowser.Document;
IHTMLElement body = (IHTMLElement)document.body;
body.insertAdjacentHTML("afterBegin", "html code");

I think this wont run the <style> tags but you can use

<div style="position:absolute;z-index:2000000;top:50%;left:50%;width:300px;height:300px;margin-top:-150px;margin-left:-150px;background:#000;">Hello World</div>

But you can't execute js this way, I found a workaround for this, you can insert an iframe tag and set your js in the onload="" tag, that way the browser will execute your js. Or you can use:

document.parentWindow.execScript("alert('a')");

I used it to insert external js files once and I think it worked fine.

Sebastian