473,396 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Determine proper window by element.

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
6 1553

The object 'window' from the event, refer to the current window, no need to
walk Up to it.
Danny
Sep 27 '05 #2
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Hennie de Nooijer | last post by:
Hi, This is a diffcult issue to explain. I hope to make my problem clear to you. SITUATION I'm building A SLA Query for a customer. This customer has an awkward way to determine the SLA results...
1
by: Timo | last post by:
All my font-sizes are set as relative sizes in CSS (large, medium, small, x-small, etc). Let's say something is set in CSS to be xx-large, but a visually impaired user wants it displayed even...
2
by: JTrigger | last post by:
I am rather new to the XML and XSD world and was wondering what the code would look like to write the following XSD items as classes in C# with all the proper XML attributes to make them...
21
by: Joakim Hove | last post by:
Hello, I have implemented a small library with a function a datatype to manage temporary storage, and handle out correctly casted storage. The function to get a double pointer is for instance: ...
3
by: Jordan | last post by:
I am dynamically inserting an html <input> tag as text (equivalent of an image button) into a page via a Literal control. Something like this gets inserted: <input type="image"...
8
by: johnsonholding | last post by:
Here is the code for a pop-up window that works in Firefox and not in IE - I get a java error or something, Here is the code : </script> <SCRIPT language="JavaScript"...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
24
by: Jeremy J Starcher | last post by:
While reading c.l.j, I've noticed that some people prefer and indeed even recommend the use of "window.alert()" over "alert()". I can't find any technical reason to make this distinction, and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.