By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,817 Members | 2,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,817 IT Pros & Developers. It's quick & easy.

Determine proper window by element.

P: n/a
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

Sep 27 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

The object 'window' from the event, refer to the current window, no need to
walk Up to it.
Danny
Sep 27 '05 #2

P: n/a
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?

Sep 27 '05 #3

P: n/a

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.
.....
}

mozilla/geckos will use 'ev' as it comes as 1st argument off the event, IE
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
work too.
Danny
Sep 28 '05 #4

P: n/a
Donius wrote:
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.
--
Rob
Sep 28 '05 #5

P: n/a
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

Sep 28 '05 #6

P: n/a
Danny wrote:
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);"

[...]

--
Rob
Sep 28 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.