1

I want to utilize events from the IE activex object and can't seem to get it to work.

Please see code below and lemme know if any idea's come to mind:

<html>
  <head>
   <title>Automate IE</title>
   <script type="text/javascript" language="javascript">
     var ie = new ActiveXObject( "InternetExplorer.Application" );
     [...some calls to ie functions...]
   </script>
  </head>
  <body>
    This is a test for IE automation.
  </body>

Now I want to be able to use events for the 'ie' object as listed here:MSDN IE Events

But can't seem to get it to work...I tried the following solutions (none worked):

Approach 1:

1. eval( "function ie::EventName(){return MyCustomEvent()}" ); - no joy )-:

Approach 2:

2. <script for="ie" event="EventName">some code here</script> - still no joy )-:

This file is saved with the 'HTA' extension - and runs with the MSHTA scripting host

Any advise \ help on how to do this would be much appreciated...thanks!

LCJ
  • 22,196
  • 67
  • 260
  • 418
user1566994
  • 304
  • 2
  • 9

2 Answers2

1

I've had success with your first method (see here).

From my experience, the parameters of the function definition must exactly match those of the event definition, e.g. for the BeforeNavigate2 event:

function ie::BeforeNavigate2(pDisp, URL, Flags, TargetFrameName, PostData, Headers, Cancel) {
    /* do stuff here */ 
}

Virtually all the Internet Explorer Application events take some parameters, and therefore your eval doesn't work.

(It's probably self-evident, but you have to fill in the actual event name; you cannot call the function whatever you want.)

Zev Spitz
  • 13,950
  • 6
  • 64
  • 136
  • Tested this today and it doesn't work. ` var ie = new ActiveXObject("InternetExplorer.application"); ie.visible = true; ie.navigate("https://google.com"); function ie::BeforeNavigate2(pDisp, URL, Flags, TargetFrameName, PostData, Headers, Cancel) { /* do stuff here */ alert('hello'); };` Returns an error complaining that 'ie' is undefined. – JonRowley Apr 07 '18 at 00:35
  • @JonRowley You need to force `= new ActiveXObject('InternetExplorer.Application');` to happen before the event handler binding, as in [this answer](https://stackoverflow.com/a/41888383/111794); one way to do this is to wrap the event handler in an `eval` statement. For a full writeup of how to attach events to an Automation source in Javascript, see [here](https://github.com/zspitz/activex-js-helpers#event-handler-management). – Zev Spitz Apr 08 '18 at 05:06
  • `var ie = new ActiveXObject("InternetExplorer.application"); ie.navigate("https://google.com"); ie.visible = true; (function(){ function ie::BeforeNavigate2(pDisp, URL, Flags, TargetFrameName, PostData, Headers, Cancel) { /* do stuff here */ alert('hello'); } })(); ` I'm unable to get this to work within a HTA either? – JonRowley Apr 09 '18 at 19:29
  • @JonRowley I'm presuming that the event handler simply doesn't fire, and you don't see any alert. What happens if you perform the navigation after the event binding: `var ie = new ActiveXObject("InternetExplorer.Application"); ie.visible = true; (function(){ function ie::BeforeNavigate2(pDisp, URL, Flags, TargetFrameName, PostData, Headers, Cancel) { alert('hello'); } })(); ie.navigate("https://google.com");` Could it be that the `BeforeNavigate` event is firing before the handler is attached? – Zev Spitz Apr 10 '18 at 05:24
  • @JonRowley To elaborate, `BeforeNavigate2` might be triggered by IE the moment a navigation is requested (e.g. via a call to the `Navigate` method), and even before IE actually starts the navigation. The event handler needs to be registered before that point. – Zev Spitz Apr 10 '18 at 05:49
0

I would have answered your question sooner but I had two kids in the last three years ;)

I don't think it is possible in an HTA anymore. ActiveXObjects have never supported events in JScript. Before IE11 you could have used VBScript and CreateObject(object, event_prefix) to register event hooks - https://msdn.microsoft.com/en-us/library/xzysf6hc(v=vs.84).aspx (And you only need to register the events in VBScript because the VBScript variables can be accessed in JavaScript.)

If IE<11 is not an option you'll need to use WScript/CScript. Here is a gist for example: https://gist.github.com/subzey/4374329

Executing WScript from an HTA is feasible with the WScript.Shell activex object, but don't get your hopes up because there is no analogous WScript.CreateObject ActiveX object (or anyway to access CreateObject() from JavaScript/JScript.)

To achieve what you want, you'd need to wrap your IE logic up in a WScript/CScript host script that monitors (or polls) a file on your hard drive. Then your HTA application can write commands to that file. If you need a feedback loop your HTA could monitor a command-result file that gets updated when the JScript logic finishes.

I've been a proponent of HTA's since the 90's, and I still use them for personal quick and dirty projects, but the writing is on the wall about their longevity. There are already a bunch of bugs related to the host window since IE10 and Microsoft has confirmed they won't be fixed.

Given that, you might want to investigate Electron as an alternative if you were not relying on IE-specific functionality: http://electron.atom.io/docs/v0.27.0/api/browser-window/

Marcus Pope
  • 2,293
  • 20
  • 25
  • Could I get your input on [my answer](http://stackoverflow.com/a/41991210/111794)? – Zev Spitz Feb 01 '17 at 22:42
  • While I didn't test your approach for the event's invocation, I did at least confirm the syntax is supported in HTA's regardless of the engine used. I certainly wish I new that bit of knowledge 10 years ago :/ Yet another example why we should never say "X can't be done." I'll edit this post and give you mad props when I get a chance to test the event invocation. – Marcus Pope Mar 22 '17 at 22:09