Trigger HTML event using WIN32OLE

D

Derek Li

Hi all,

I'm currently using WIN32OLE object to test my Web pages. What I need to
do is to trigger HTML event on specific element with the event object I
pass to the fireEvent() method. But unfortunately, I'm not able to make
it work.

Here's the HTML file(c:\\test.html, will be used in the following code):

<html>
<head>
<title>test</title>
<script language="javascript">
function handler(e){
alert(e.keycode);
}
</script>
</head>
<body onkeyup="handler(event);">
</body>
</html>


Here's the Ruby code I'm using:

require 'win32ole'
ie = WIN32OLE.new('InternetExplorer.Application')
ie.visible = TRUE
ie.gohome
ie.navigate('c:\\test.html')
event_obj = ie.document.createEventObject
event_obj.keycode = 1
ie.document.body.fireEvent('onkeyup',event_obj)
print "Now quit Internet Explorer... Please enter."
gets
ie.Quit()

The result expected is the popping up an alert window whose content is
1, which is the value of the keycode property of the event object passed
in. But the actual result is the value becomes 'undefined'. If I use
Javascript to implement the same logic, it works as expected.

Here's code of Javascript:

var e = document.createEventObject();
e.keycode = 1
document.body.fireEvent('onkeyup',e);

Does anybody know the reason why the event object created cannot be got
by the browser? Any advice would be appreciated. Thanks a lot.

-Derek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top