Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HWND of an IE Window Opened via Javascript

239 views
Skip to first unread message

RMD

unread,
Jun 6, 2003, 2:30:10 AM6/6/03
to
I need to somehow get the HWND of an IE window that was opened via a
window.open JavaScript command.

The IE window executing the JavaScript will be created via a new
ShDocVw.InternetExplorer.

Basically, I need to know if there is a way to listen for new windows being
created (via some event exposed via ShDocVw).

I can pretty much assume that my instance of ShDocVw.InternetExplorer will
*only* be opening a new instance via JavaScript, so any new windows spawned
from that instance can be assumed to be generated from the JavaScript
command.

Ideally, ShDocVw.InternetExplorer would expose an event that fired any time
a new window was generated from that instance of IE. The event would have an
argument containing the HWND of the new window. Alas, I don't really see any
event like this.

Any ideas?

RMD


Csaba2000

unread,
Jun 6, 2003, 3:20:52 AM6/6/03
to
The way I am matching up DOM elements to their appropriate
IE instance is:

set Wnds = CreateObject("Shell.Application")
For each oWnd in Wnds.windows
'Or do you want oWnd.hWnd?
If TypeName(oWin.document) = "HTMLDocument" Then
If oWin.document.body Is myInstanceTopLevelBody
...
End If
End If
Next

If you happen to have ahold of a DOM element within a frame,
then it's a bit stickier getting to the top level window.

Csaba Gabor from New York

"RMD" <rmd@ask_me_if_you_want_it.ok> wrote in message news:ODrWXU$KDHA...@TK2MSFTNGP09.phx.gbl...

RMD

unread,
Jun 6, 2003, 9:32:51 AM6/6/03
to
I'm somewhat confused by your example.

If the oWnd in question is the popup I'm concerned with,
"oWin.document.body" will evaluate to the body property of the window that
caused the popup?

So if i do something like:

set objOriginalDoc = objIE.document;
'Do your loop...
if oWin.document.body is objOriginalDoc.body then
'.. we've found the window that was caused by the JavaScript window.open
command

Correct?

RMD


"Csaba2000" <ne...@CsabaGabor.com> wrote in message
news:ubGsYt$KDHA...@tk2msftngp13.phx.gbl...

Csaba2000

unread,
Jun 6, 2003, 12:40:08 PM6/6/03
to
Correct, as I understand your question
I have a note to myself from a while back that says comparing the
two .document properties can cause problems. But I've forgotten why.

Csaba

"RMD" <rmd@ask_me_if_you_want_it.ok> wrote in message news:#SK0iADL...@tk2msftngp13.phx.gbl...

RMD

unread,
Jun 6, 2003, 11:05:54 PM6/6/03
to
Ok, great, I'll give that a shot.

So a popup window caused by javascript doesn't have its own document.body?

RMD


"Csaba2000" <ne...@CsabaGabor.com> wrote in message

news:%23dtO6lE...@TK2MSFTNGP09.phx.gbl...

RMD

unread,
Jun 6, 2003, 11:57:45 PM6/6/03
to
The code you gave doesn't seem to work.

After further investigation, I discovered the
InternetExplorer.Document.ParentWindow.Opener property.

This seemed to be exactly what I needed, but it doesn't seem to be populated
with anything for my popup window. Shouldn't it point to the instance of IE
that ran the javascript which opened it?

I even tried modifying my javascript so that it explicitly set the popup
window's opener property like:

var win = window.open(...);
win.opener = this;

But that doesn't seem to make any difference.

What am I doing wrong?

RMD


"Csaba2000" <ne...@CsabaGabor.com> wrote in message

news:%23dtO6lE...@TK2MSFTNGP09.phx.gbl...

Csaba2000

unread,
Jun 7, 2003, 2:51:17 AM6/7/03
to
Since you are using window.open, .opener should be set.
To see whether .opener is being set properly,
I assume you have an alert statement in the onLoad function
of the newly opened window that says something like:
<body onload='alert ("original window: " + window.opener.document.title)'>
In fact, I suggest being very liberal with alert statements.
You don't get a handle or instance to your IE; that's why you have to loop
through all the instances.

Perhaps post a bit more of your code. "doesn't seem to work" and
"doesn't seem to make any difference" are too vague for me.
Let's see the part that's doing the creation and the part that's
doing the testing.

Csaba

"RMD" <rmd@ask_me_if_you_want_it.ok> wrote in message news:OPgT0jKL...@tk2msftngp13.phx.gbl...

RMD

unread,
Jun 7, 2003, 2:51:33 AM6/7/03
to
I ended up giving up on this method.

Instead, I handle the NewWindow2 event from the instance of IE that's
loading the JavaScript.

When the event fires, I create a new instance of IE and save a reference to
it, then I set the ppDisp object's reference equal to the IE instance I just
created.

I can then close the popup whenever I want by just calling the Quit method
of my IE instance.

Once I launch the instance of IE that loads the JavaScript, I just have to
enter a while loop, call DoEvents, sleep for a few milliseconds, then repeat
the loop until my reference is set by the event handler. Once it is, I exit
the loop and continue on with my application.

Thanks for your help.

RMD

"Csaba2000" <ne...@CsabaGabor.com> wrote in message

news:%23Vn4iBM...@tk2msftngp13.phx.gbl...

Csaba2000

unread,
Jun 7, 2003, 3:17:41 AM6/7/03
to
Would you mind posting, please, the call that you are using to
create the window and also the declaration and code for your
NewWindow2 handling? I'd like to see the details.
I'm not understanding why you are creating a new instance of IE
when the window.open is creating one for you. Probably a look
at the relevant part of your code will make it clear. Perhaps
it will give me an idea for a related issue I'm having.

Csaba

"RMD" <rmd@ask_me_if_you_want_it.ok> wrote in message news:#Tne7EML...@tk2msftngp13.phx.gbl...

0 new messages