Reputation: 304
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!
Upvotes: 1
Views: 2283
Reputation: 15327
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.)
Upvotes: 1
Reputation: 2293
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/
Upvotes: 0