开发者

How to create a floating div in BHO dynamically?

开发者 https://www.devze.com 2022-12-08 19:26 出处:网络
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 sh

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消