Determine proper window by element.

D

Donius

Is there a simple and clean way to get the window object that contains
an element that is passed into a function? I want to throw some alerts
/ confirms, but don't want to switch away from whatever window they're
currently looking at.

I can step up by parentNode until the nodeType equals 9, which means
the document element, and then step up one more...does that ensure a
window object? Or i could step up until the node had no
parentNode...would that be the window object?

Any other ideas anyone?

-Brendan
 
D

Donius

Hmm...a good thought...i can't just call event like this though:
var cwin = event.window;

because 'event is not defined'. I'm doing something where i have (a) a
main page and (b) some number of windows that are opened from that
page, and the opened windows can all call functions from the main page.
So when i'm doing this, window.event doesn't hold the last event on
one of the opened windows, it holds the last event on the main window,
and i want the window where the event happened. Currently the
functions pass just themselves (like: onclick="functioncall(this);").
Is there a way to pass the event with the function? Does self.event
make sense in that context?

So, in general: How would i get the event?
 
R

RobG

Donius said:
Is there a simple and clean way to get the window object that contains
an element that is passed into a function?

Yes. Pass a reference to the window when you call the function in the
opener. e.g. in the child window do:

opener.someFn(window, parm1, parm2);

I want to throw some alerts
/ confirms, but don't want to switch away from whatever window they're
currently looking at.

Use the alert (or prompt or whatever) method of the window whose
reference you've been passed - quick 'n dirty script below:

<script type="text/javascript">
var newWin;
function openWin()
{
newWin = window.open('', 'NewWindow',
'menubar,location,resizable,scrollbars,status,'
+'height=500,width=600');

newWin.document.open();
newWin.document.write(
'<html><title>Child of NewWindow</title>',
'<body><input type="button" value="Show alert" ',
'onclick="opener.parentAlert(window, \'hi from child\');">',
'</body></html>'
);
newWin.document.close();
newWin.focus();
}

function parentAlert( win, txt )
{
win.alert( 'Alert from opener: ' + txt );
}

</script>
<input type="button" value="Open window" onclick="
openWin();
">
I can step up by parentNode until the nodeType equals 9, which means
the document element, and then step up one more...does that ensure a
window object? Or i could step up until the node had no
parentNode...would that be the window object?

Just use window, no DOM climbing required. I think the highest node is
'document' or the HTML element, but it's not required.
Any other ideas anyone?

Yeah, but I think the one above is best.
 
D

Donius

Thanks to both Rob & Danny, that cleared things up. :) I started
passing in the window object as my final solution. I appreciate the
help!

-Brendan
 
R

RobG

Danny said:
Event is at the window/frame level, so, self.event is fine, however, event
IS defined, in IE, but in mozilla you have to use the 1st argument or
arguements[0] to get the event object in the function, what I've used is:

function SOSO(ev) {
if (window.external) ev=window.event; // IE is the only
browser with window.external object.

If you want to test for window.event, then test for exactly that:

ev = ev ||window.ev
.....
}

mozilla/geckos will use 'ev' as it comes as 1st argument off the event, IE

It it will be whichever paramater you pass it as, it can be in any position.
will use window.event, or self.event if you wish, another thing is, yes,
you can pass 'event' as an argument, so onclick="SOSO(this.event)" should

onclick="SOSO(event);"

[...]
 

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,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top