Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Main window call Javascript function in popup window, and vice versa (http://www.velocityreviews.com/forums/t932472-main-window-call-javascript-function-in-popup-window-and-vice-versa.html)

Jimmy 08-16-2007 01:45 PM

Main window call Javascript function in popup window, and vice versa
 
It is also possible for popup window to call function in main window
by using the opener property. Will "opener.someFunctionInMain(param1,
param2)" in the popup window work?

It's possible for main window to call function in the popup window,
right? The following is a sample code (close popup window causes to
show alert window) which doesn't seems to work. Can anyone see the
problem?

// main
window /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

<html><head></head><body onLoad="testPopup()">
<script type="text/javascript">
<!--
var popupWindow;

function testPopup() {
popupWindow = window.open("child.html", "Pop", "width=700,
height=400, scrollbars=1, dependent=yes");
popupWindow.focus();
}

// NOTE this one doesn't work in Mozilla and just commented out
//popupWindow.onunload = function() {
// alert("closing child");
//}

// NOTE this doesn't seems to work either
popupWindow.onunload = doOnunload;
function doOnunload() {
// expecting alert window will show up when popup window close
alert("closing child");
}
-->
</script>
</body></html>

// popup
window /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

<html><head></head>
<body>
<form id="blahForm">
<input type="hidden" id="blahId" value="blahValue"/>
</form>
</body>
</html>


josh 08-16-2007 02:00 PM

Re: Main window call Javascript function in popup window, and vice versa
 
On 16 Ago, 15:45, Jimmy <jimmy_ple...@yahoo.com> wrote:
> It is also possible for popup window to call function in main window
> by using the opener property. Will "opener.someFunctionInMain(param1,
> param2)" in the popup window work?
>
> It's possible for main window to call function in the popup window,
> right? The following is a sample code (close popup window causes to
> show alert window) which doesn't seems to work. Can anyone see the
> problem?
>
> // main
> window /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
>
> <html><head></head><body onLoad="testPopup()">
> <script type="text/javascript">
> <!--
> var popupWindow;
>
> function testPopup() {
> popupWindow = window.open("child.html", "Pop", "width=700,
> height=400, scrollbars=1, dependent=yes");
> popupWindow.focus();
>
> }
>
> // NOTE this one doesn't work in Mozilla and just commented out
> //popupWindow.onunload = function() {
> // alert("closing child");
> //}
>
> // NOTE this doesn't seems to work either
> popupWindow.onunload = doOnunload;
> function doOnunload() {
> // expecting alert window will show up when popup window close
> alert("closing child");}
>
> -->
> </script>
> </body></html>
>
> // popup
> window /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
>
> <html><head></head>
> <body>
> <form id="blahForm">
> <input type="hidden" id="blahId" value="blahValue"/>
> </form>
> </body>
> </html>


I post the right code:
<html><head></head><body onLoad="testPopup()">
<script type="text/javascript">
<!--
var popupWindow;

function testPopup()
{
popupWindow = window.open("child.html", "Pop", "width=700,
height=400, scrollbars=1, dependent=yes");
popupWindow.focus();

// put here.....anonymous works on MOZILLA!!!!
popupWindow.onunload = function()
{
alert("closing child");
}

}
-->
</script>
<body onload="testPopup()">
</body>
</html>

Bye


Thomas 'PointedEars' Lahn 08-16-2007 02:24 PM

Re: Main window call Javascript function in popup window, and viceversa
 
josh wrote:
> I post the right code:


What is supposed to be "right" about this code is beyond me.

> <html><head></head><body onLoad="testPopup()">


http://validator.w3.org/

> <script type="text/javascript">
> <!--


It is unnecessary, and it is error-prone to try to "comment out" script
element content. Worst case: syntax error.

> var popupWindow;
>
> function testPopup()
> {
> popupWindow = window.open("child.html", "Pop", "width=700,
> height=400, scrollbars=1, dependent=yes");


window.open() is a host object's method that should be feature-tested before
being called.

"Pop" maybe is used already as window name, so the respective window will be
reused. This can be prevented if a timestamp, being a unique number over
time, is prefixed, infixed or suffixed to the window name.

The feature string must not contain spaces. It has to be `scrollbars'
(without `=1') to be compatible. `dependent' (without `=yes') suffices.
IMHO, a popup should always be `resizable' as well; scrollbars sometimes
fail to do their job.

> popupWindow.focus();


`popupWindow' may not return a Window object reference, so it should be
subject to a type-converting test. Window::focus() is a host object's
method that should be feature-tested for.

It may not even work because current browsers have an option to prevent
windows from being focused by scripting. (IIRC, it was discussed recently
here.)

> // put here.....anonymous works on MOZILLA!!!!


Your Exlamation Mark key is borken.

> popupWindow.onunload = function()
> {
> alert("closing child");
> }


This *may* work.

> }
> -->


This line is unecessary and error-prone; `-', `--' and `>' are operators.

> </script>
> <body onload="testPopup()">


This unrequested popup will probably not open because of a popup blocker.
Furthermore, there is a duplicate `body' element, or a `script' element
outside the `body' element (whatever way you want it), which is not Valid
markup (see above.)

> </body>
> </html>


Please *don't reply* if you lack the minimum clue. Nobody is helped
by that (except maybe yourself). Thank you for your attention.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>

Henry 08-16-2007 02:48 PM

Re: Main window call Javascript function in popup window, and vice versa
 
On Aug 16, 3:24 pm, Thomas 'PointedEars' Lahn wrote:
> josh wrote:
>> function testPopup()
>> {
>> popupWindow = window.open("child.html", "Pop", "width=700,
>> height=400, scrollbars=1, dependent=yes");

>

<snip>
> > // put here.....anonymous works on MOZILLA!!!!

>
> Your Exlamation Mark key is borken.
>
>> popupWindow.onunload = function()
>> {
>> alert("closing child");
>> }

>
> This *may* work.

<snip>

It seems unlikely that it would. There will be a timing issue where
the new browser window is opened and makes its HTTP request to the
server, the script goes on to execute the above and attach an onunload
handler to the window object of the new browser window, and then the
HTTP response arrives, loads a new page, and clears all of the
listeners previously associated with the window, along with all the
other script assigned properties of the window object.



All times are GMT. The time now is 09:52 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.