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
Sergey,

I'm not that familiar with c# (really completely unfamiliar). That's why the code example is giving me issues. It's familiar enough to make out what's sort of going on, but the objects are different enough in VB.Net that I'm having issues converting it.

Sorry for the confusion,
Jeff.
Posted 02 Aug, 2011 11:33:03 Top
Jeff Hafer




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

I decided to use the example given on this page:

http://www.add-in-express.com/docs/internet-explorer-html-events.php

It is for VB.Net and is very simple to understand. However, I never have any success. The webpage always displays "The add-on isn't registered.".

Any idea why the add-on doesn't get registered???? I am testing the project by using "Register ADX Project" manually in VS 2010.

=====================================================================================
Here's the code for reference. I included the MyWinApiWindow class at the bottom:
=====================================================================================
Imports System.Windows.Forms
Imports AddinExpress.IE
Imports System.Reflection
'...
Private Sub IEToolbar_DownloadComplete() Handles MyBase.DownloadComplete
If Not ThisIsMyPage(Me.HTMLDocument.url) Then Return
Try
Dim scriptEngine As Object = Me.HTMLDocument.Script
scriptEngine.GetType().InvokeMember("myAddon",_
BindingFlags.SetProperty, Nothing, scriptEngine, New Object() {Me})
Catch ex As Exception
End Try

'<ht ml><head><sc ript type="text/javascript">
'var myAddon = null;
'function MyFunction()
'{
' if(myAddon != null)
' {
' myAddon.MyMethod("okay");
' }
' else
' alert("The add-on isn't registered");
'}
'</script></head><body>
'<input type="button" name="button1" value="Click me!" onc lick="MyFunction();">
'</body></html>
End Sub

Public Sub MyMethod(param As String)
MessageBox.Show(New MyWinApiWindow(Me.ParentHandle), param)
End Sub

Public Class MyWinApiWindow
Implements System.Windows.Forms.IWin32Window

Dim theHandle As IntPtr

Public Sub New(ByVal aHandle As System.IntPtr)
theHandle = aHandle
End Sub

Public ReadOnly Property Handle() As System.IntPtr _
Implements System.Windows.Forms.IWin32Window.Handle
Get
Return theHandle
End Get
End Property
End Class
Posted 02 Aug, 2011 13:48:54 Top
Mangesh Paranjape




Posts: 5
Joined: 2011-07-26
Hi,
Yes, you are right. because of vb.net, I don't see all the mshtml properties. the code did not work initially because of the way event handling was coded in vb.net. I changed it and defined event handler for document_complete in Initialize function and then it started working. so all set for now.
Posted 02 Aug, 2011 21:13:49 Top
Mangesh Paranjape




Posts: 5
Joined: 2011-07-26
Hi Jeff,
I was facing the same issue with vb.net. I would suggest you to change the event handler code and put that in Initialize function. It started working for me after doing this change. If you want I can send you my vb.net project.
Posted 02 Aug, 2011 21:18:18 Top
Sergey Grischenko


Add-in Express team


Posts: 7224
Joined: 2004-07-05
Hi guys,

Mangesh, thank you for the help. Jeff, let me know if the issue still exists.
Posted 03 Aug, 2011 03:46:38 Top
Jeff Hafer




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

It would be great if you could post the code! I thank you for the help.

Jeff.
Posted 03 Aug, 2011 05:45:34 Top
Jeff Hafer




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

I got it! Used the following sub instead of IEToolbar_DownloadComplete():


Public Sub Initialize() Handles MyBase.DownloadComplete
'If Not ThisIsMyPage(Me.HTMLDocument.url) Then Return

Try
Dim scriptEngine As Object = Me.HTMLDocument.Script
scriptEngine.GetType().InvokeMember("myAddon", BindingFlags.SetProperty, Nothing, scriptEngine, New Object() {Me})
Catch ex As Exception
End Try
End Sub
Posted 03 Aug, 2011 08:38:54 Top
Jeff Hafer




Posts: 12
Joined: 2010-09-21
Thanks everyone for helping me with this issue!

Jeff.
Posted 03 Aug, 2011 08:40:45 Top
Mangesh Paranjape




Posts: 5
Joined: 2011-07-26
Another issue: New functions added to IEModule are not getting recognised by javascript. My guess is the IEModule is not getting updated or the old copy is cached / locked. What could be the issue?
I tried unregistering/ registering but no luck.
Posted 03 Aug, 2011 18:24:49 Top
Jeff Hafer




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

To be really sure the IEModule is unregistered, you can use HijackThis from TrendMicro. It lets you do a quick system scan (5 seconds or so) and displays all the BHO's, Toolbars, etc. If you do see one there, just check it and choose "Fix It" and it will be removed. Here's a link to HijackThis:

http://free.antivirus.com/hijackthis/

Hope this helps,
Jeff.
Posted 04 Aug, 2011 05:53:19 Top