Adding JS to the webpage on DocumentComplete for IE8

Add-in Express™ Support Service
That's what is more important than anything else

Adding JS to the webpage on DocumentComplete for IE8
How to run JS on DocumentComplete 
Rabbit


Guest


I am currently editing IEModule.cs in order to include and run a block of JavaScript on page-and-tab load-or-reload but, since I have no prior experience of writing C# .NET IE add-ons, I have a few questions that I need some guidance on please:

This seems to be working so far but, just to check, can I access the HTML head DOM for the currently loaded tab/page using the following two lines or is there a better option?

mshtml.HTMLDocument doc = this.HTMLDocument;

IHTMLElement headElem = (IHTMLElement)((IHTMLElementCollection)doc.all.tags("head")).item(null, 0);


Do I need a call for DownloadComplete (as well as DocumentComplete) if I want the add-on to run on tab/page load or reload? or was this requirement purely for older versions of Add-in Express?

Also, what is the best way to load/save variables using C# .NET, passing the results to/from JavaScript on the page? I'm sure I saw something on the topic earlier but can't find the article again at the moment.

Cheers,
Posted 04 Feb, 2011 13:18:51 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Rabbit.

mshtml.HTMLDocument doc = this.HTMLDocument;
IHTMLElement headElem = (IHTMLElement)((IHTMLElementCollection)doc.all.tags("head")).item(null, 0);

The code is correct. You can also use the getElementsByTagName method of the HTML document.

Do I need a call for DownloadComplete (as well as DocumentComplete) if I want the add-on to run on tab/page load or reload? or was this requirement purely for older versions of Add-in Express?

The DownloadComplete and DocumentComplete events are called automatically by IE. You don't need to call them.

To access JavaScript variables from the .NET code you can use the late binding.
E.g.
private void IEModule_DownloadComplete()
{
try
{
object scriptEngine = HTMLDocument.Script;
if (scriptEngine != null)
{
scriptEngine.GetType().InvokeMember("bhoModule", BindingFlags.SetProperty, null, scriptEngine, new object[] { this } );
}
}
catch
{
}
}
Posted 07 Feb, 2011 04:36:39 Top
Rabbit


Guest


Sorry, I meant handle/bind the events not call, and thank you for your reply.
Posted 08 Feb, 2011 08:15:04 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
You can connect to any IE events at the design-time (click on the iemodule and go to the Events tab of the Properties window).
Posted 08 Feb, 2011 10:34:31 Top