window.open then onload function

R

Richard Bell

I'm a bit new to Javascript and am trying to do

oNewWin = window.open( ... );

then determine when the contents of oNewWin are completely loaded.

I've tried

oNewWin.attachEvent( "onload", myfunc);

but that doesn't work.

Can something of this sort be done?
 
G

Grant Wagner

Richard said:
I'm a bit new to Javascript and am trying to do

oNewWin = window.open( ... );

then determine when the contents of oNewWin are completely loaded.

I've tried

oNewWin.attachEvent( "onload", myfunc);

but that doesn't work.

Can something of this sort be done?

Create a dummy document as follows:

--dummy.html--
<html>
<head>
<title>dummy.html</title>
</head>
<body onload="
if (window.opener && window.opener.callBack) {
window.opener.callBack();
}
">
</body>
</html>

Then structure your script to open the new window as follows:

<script type="text/javascript">
function callBack() {
alert('The new window has finished loading!');
}
oNewWin = window.open('dummy.html', 'theName', '...');
</script>

If you don't like the idea of creating dummy.html on your file system,
you could use:

var newDocument = [
'<html>',
'<head>',
'<title>dummy.html</title>',
'</head>',
'<body',
' onload="if (window.opener && window.opener.callBack)',
' window.opener.callBack();">',
'</body>',
'</html>'
].join('\n');
oNewWin = window.open('javascript:newDocument', 'theName', '...');

All the usual warnings about popups apply (they may not be available,
they may be available but not do what you expect, they may open as new
tabs in an existing browser window, etc).
 
R

Richard Bell

Create a dummy document as follows:

--dummy.html--
<html>
<head>
<title>dummy.html</title>
</head>
<body onload="
if (window.opener && window.opener.callBack) {
window.opener.callBack();
}
">
</body>
</html>

Then structure your script to open the new window as follows:

<script type="text/javascript">
function callBack() {
alert('The new window has finished loading!');
}
oNewWin = window.open('dummy.html', 'theName', '...');
</script>

Unfortunately, I need to load an arbitrary URL that I've no control
over, thus I was hoping to sit on the child window's onload event or
something similar. I did find a frame approach that works for many,
but alas not all ... about a 6% failure rate, URLs and am searching
for a more robust solution.

Is anyone aware of an approach that doesn't depend on frames or having
access to the childs HTML content?

In a somewhat related question, how do I tell that the child has
created a child (popup) window of its own?

Thanks
If you don't like the idea of creating dummy.html on your file system,
you could use:

var newDocument = [
'<html>',
'<head>',
'<title>dummy.html</title>',
'</head>',
'<body',
' onload="if (window.opener && window.opener.callBack)',
' window.opener.callBack();">',
'</body>',
'</html>'
].join('\n');
oNewWin = window.open('javascript:newDocument', 'theName', '...');

All the usual warnings about popups apply (they may not be available,
they may be available but not do what you expect, they may open as new
tabs in an existing browser window, etc).
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top