Execute a function from Javascript/HTML?

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

Execute a function from Javascript/HTML?
 
Jeff Hafer




Posts: 12
Joined: 2010-09-21
In addition to menu items and a toolbar, I'd like to be able to take some specific action when a user clicks different buttons. I'm hoping there is a way to access some of the Add-In Express functions from Javascript. I'm specifically programming for XP SP3 clients right now. I'm hoping there is something available like an ActiveXObject call:

var myobject;
myobject = new ActiveXObject("testLibrary.CoTest");
alert(myobject.TestMethod());

I've looked through the forum for information, but nothing matched.

Thanks in advance for any help,
Jeff.
Posted 01 Aug, 2011 09:14:41 Top
Andrei Smolin


Add-in Express team


Posts: 18794
Joined: 2006-05-11
Hello Jeff,

Please find the string "Module-script two-way interaction" in http://www.add-in-express.com/docs/internet-explorer-html-events.php.

Is this what you are looking for?


Andrei Smolin
Add-in Express Team Leader
Posted 01 Aug, 2011 09:25:03 Top
Jeff Hafer




Posts: 12
Joined: 2010-09-21
Andrei,

Wow! That was a quick response.

I think you may have directed me to the correct answer. I will check this out and update this topic as soon as I know.

Thank you very much!
Posted 01 Aug, 2011 09:29:37 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Jeff,

You don't need an ActiveX object. Please try the code below:

The add-on code:

private void IEModule_DownloadComplete()
{
try
{
object scriptEngine = HTMLDocument.Script;
if (scriptEngine != null)
{
scriptEngine.GetType().InvokeMember("bhoModule", BindingFlags.SetProperty, null, scriptEngine, new object[] { this } );
}
}
catch
{
}
}

public void AddRows(int rowCount, int colCount)
{
mshtml.HTMLDocument document = HTMLDocument;
mshtml.IHTMLTable table = document.getElementsByTagName("TABLE").item(null, 0) as mshtml.IHTMLTable;
if (table != null)
{
mshtml.HTMLTableSection tbody = table.tBodies.item(null, 0) as mshtml.HTMLTableSection;

if (tbody != null)
{
for (int r = 0; r < rowCount; r++)
{
mshtml.IHTMLDOMNode tr =
document.createElement("tr") as mshtml.IHTMLDOMNode;

for (int c = 0; c < colCount; c++)
{
mshtml.IHTMLDOMNode td = document.createElement("td") as mshtml.IHTMLDOMNode;
mshtml.IHTMLDOMNode newText = document.createTextNode((c + r).ToString()) as mshtml.IHTMLDOMNode;

td.appendChild(newText);
tr.appendChild(td);
}

tbody.appendChild(tr);
}

}
}
}


The HTML test page:

<html>
<head>
<script type="text/javascript">

var bhoModule = null;

function createTable(data)
{
var table = document.createElement("table");
var thead = document.createElement("thead");
var tr = document.createElement("tr");

for (var i = 0; i < data[0].length; i++)
{
var th = document.createElement("th");
var newText = document.createTextNode(data[0][i]);

th.appendChild(newText);
tr.appendChild(th);
}

thead.appendChild(tr);
table.appendChild(thead);

var tbody = document.createElement("tbody");

for (var i = 1; i < data.length; i++)
{
var tr = document.createElement("tr");

for (var j=0; j < data[i].length; j++)
{
var td = document.createElement("td");
var newText = document.createTextNode(data[i][j]);

td.appendChild(newText);
tr.appendChild(td);
}

tbody.appendChild(tr);
}

table.appendChild(tbody);
return table;
}

function fill_table()
{
if (bhoModule != null)
{
var table = createTable([ ["one", "two", "three", "four", "five"] ]);
document.body.appendChild(table);

bhoModule.AddRows(10, 5);
}
else
{
document.write("BHO is not installed.");
}
}

</script>
</head>

<body onload="fill_table()">

<p>Add-in Express example: How to access an IE module from javascript.</p>

</body>
</html>
Posted 01 Aug, 2011 09:30:05 Top
Mangesh Paranjape




Posts: 5
Joined: 2011-07-26
We have started exploring this tool and the first impression is very good. This is a great framework.

I have one small issue:
This code to access BHO from JS does not work for me. Also I found that there is nothing called "Script" object but there is "Scripts" object. May be I am missing something.

Exception is : "Method 'System.String.bhoModule' not found."
Posted 02 Aug, 2011 10:05:43 Top
Jeff Hafer




Posts: 12
Joined: 2010-09-21
Sergey,

I'm using VB.Net and the code does not work for me. I'm unsure most of all about the line following line your example:

scriptEngine.GetType().InvokeMember("bhoModule", BindingFlags.SetProperty, null, scriptEngine, new object[] {

Thanks,
Jeff.
Posted 02 Aug, 2011 10:45:35 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Mangesh,

Did you define the 'bhoModule' variable in the javascript code?

Also I found that there is nothing called "Script" object but there is "Scripts" object

I guess that you use VB.NET. That is why you don't see all mshtml properties. Just type 'HTMLDocument.Script' in your code. It should work.
Posted 02 Aug, 2011 11:13:33 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Jeff,

What do you mean saying 'the code does not work for me'? Is it not compilable or you just don't understand it?

scriptEngine.GetType().InvokeMember("bhoModule", BindingFlags.SetProperty, null, scriptEngine, new object[] { this } );

This line of the code initializes the 'bhoModule' javascript variable with an instance of the iemodule. The 'bhoModule' variable should be defined (or injected) in the HTML source code before you call the InvokeMember method.
Posted 02 Aug, 2011 11:20:25 Top
Jeff Hafer




Posts: 12
Joined: 2010-09-21
Sergey,

I guess the biggest problem is the braces. I'm guessing the code isn't VB.Net?

Jeff.
Posted 02 Aug, 2011 11:26:36 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Yes, the code is written in C#.
Posted 02 Aug, 2011 11:30:07 Top