problem calling function in child window

R

rob

Hello,

I am opening a child window, and populating it with a document.write.
In the document.write text is a javascript function, which I call
immediatly after I call the document.write in the parent function.
Firefox complains that the function in the child window "is not a
function". However, if I call that same child function later from
another event, it works fine. Any idea what would cause this? Here
is what I am doing in code:

var myWindow = null;

function show_new_win() {
var content = "some content";
if (myWindow==null) {

myWindow=window.open('','OrigFileWindow','width=1200,height=1200');

myWindow.document.write("<html><head><link type='text/css'
rel='stylesheet'
href='style.css'><script> function
mytest(id, pos){var newdiv =
document.getElementById(id);newdiv.className='highlightcnum';var
outerdiv = document.getElementById('innerfile');
outerdiv.scrollTop=pos; }<\/script></head><body >" + content + "</
body></html>");
}
myWindow.mytest("h_" + mycnum ,500);
}

Thanks for any insights.

}
 
L

Lee

rob said:
Hello,

I am opening a child window, and populating it with a document.write.
In the document.write text is a javascript function, which I call
immediatly after I call the document.write in the parent function.
Firefox complains that the function in the child window "is not a
function". However, if I call that same child function later from
another event, it works fine. Any idea what would cause this? Here
is what I am doing in code:

var myWindow = null;

function show_new_win() {
var content = "some content";
if (myWindow==null) {

myWindow=window.open('','OrigFileWindow','width=1200,height=1200');

myWindow.document.write("<html><head><link type='text/css'
rel='stylesheet'
href='style.css'><script> function
mytest(id, pos){var newdiv =
document.getElementById(id);newdiv.className='highlightcnum';var
outerdiv = document.getElementById('innerfile');
outerdiv.scrollTop=pos; }<\/script></head><body >" + content + "</
body></html>");
}
myWindow.mytest("h_" + mycnum ,500);
}

Thanks for any insights.

When you invoke window.open(), the call may return before the
window actually appears, so you're taking a chance by writing
into its document immediately.

On top of that, the document.write() call returns as soon as
it's handed off the HTML to the new window. At the time that
you're trying to invoke the new function, it may not have even
been parsed yet.


--
 
R

rob

rob said:










When you invoke window.open(), the call may return before the
window actually appears, so you're taking a chance by writing
into its document immediately.

On top of that, the document.write() call returns as soon as
it's handed off the HTML to the new window. At the time that
you're trying to invoke the new function, it may not have even
been parsed yet.

--

Ok Lee, thanks.

Is there a standard best practice for doing as I describe? If not,
would you suggest some type of 'sleep()' function between calls?

Thanks again

-Rob
 
R

rob

rob said:










When you invoke window.open(), the call may return before the
window actually appears, so you're taking a chance by writing
into its document immediately.

On top of that, the document.write() call returns as soon as
it's handed off the HTML to the new window. At the time that
you're trying to invoke the new function, it may not have even
been parsed yet.

--

So I did put a wait() function after window.open and document.write.
When I run it in IE, it works as I want it to, but still does not in
Firefox.

One funny thing about Firefox is that after the content is written and
it shows up, the browser still acts like it is loading the page. The
status bar says "Reading myurl", and the mouse pointer is hourglass.
IE doesn't do anything like this. Is there something I need to send
the child window to indicate the window should be totally rendered?

Thanks

-Rob
 
R

rf

So I did put a wait() function after window.open and document.write.
When I run it in IE, it works as I want it to, but still does not in
Firefox.

I doubt this (the IE bit) as there is no "wait()" function in javascript.
 
R

RobG

It would be much better if content was passed to the function, e.g.

function show_new_win(content){

The faq suggests:

if (!myWindow || myWindow.closed) {



That is a very unfriendly size, use something like 400x300.

That is doomed as you don't create any elements with ids in the new
window. You should post examples that are likely to "work" with
minimal effort. At least add some resilance to the function:


innerfile isn't defined anywhere either, more to fix. Is it necessary
for the example?


It is much easier for others if you format that for posting.


mycnum isn't defined either, more for me to fix... :-(


Yes, though it's not usually a problem.


Yes again.

So I did put a wait() function after window.open and document.write.
When I run it in IE, it works as I want it to, but still does not in
Firefox.

Don't use a "wait" (which I presume uses some guess based on
setTimeout), have a function in the child window call back to the
parent when it is ready.

One funny thing about Firefox is that after the content is written and
it shows up, the browser still acts like it is loading the page.

Because you haven't called document.close() when you've finished with
document.write (i.e. finished writing to the document).

The
status bar says "Reading myurl", and the mouse pointer is hourglass.
IE doesn't do anything like this. Is there something I need to send
the child window to indicate the window should be totally rendered?

Yes, when you've finished writing to the child window, call its
document.close method, e.g.:

<script type="text/javascript">

var myWindow = null;

function show_new_win(content) {
var content = content || '<div>No content&hellip;</div>';
if (!myWindow || myWindow.closed) {
myWindow=window.open('','OrigFileWindow',
'width=400,height=300');

// 1200x1200 is a bit unfriendly...

myWindow.document.write(
"<html><head><link type='text/css'"
+ " rel='stylesheet' href='style.css'>"
+ "<script>"
+ "function mytest(id, pos){"
+ " var newdiv = document.getElementById(id);"
+ " if (newdiv) {"
+ " newdiv.className='highlightcnum';"
+ " }"
+ " var outerdiv = document.getElementById('innerfile');"
+ " if (outerdiv) {"
+ " outerdiv.scrollTop=pos;"
+ " }"
+ " alert('Hey, it worked');"
+ "}"
+ "window.onload = function(){opener.popWinReady();}"
+ "<\/script></head><body >"
+ content
+ "<\/body><\/html>"
);
myWindow.document.close();
}
}

function popWinReady() {
var mycnum = 10;
myWindow.mytest("h_" + mycnum ,500);
}

show_new_win('<div id="h_10">I am a div h_10</div>');

</script>
 
R

rob

I doubt this (the IE bit) as there is no "wait()" function in javascript.

Hi Richard, yeah no builtin wait() function but, was trying something
like this:

function wait(msecs)
{
var start = new Date().getTime();
var cur = start
while(cur - start < msecs)
{
cur = new Date().getTime();
}
}

But not optimal in any event.

Thanks
 
E

Erwin Moller

rob said:
Hello,

I am opening a child window, and populating it with a document.write.
In the document.write text is a javascript function, which I call
immediatly after I call the document.write in the parent function.
Firefox complains that the function in the child window "is not a
function". However, if I call that same child function later from
another event, it works fine. Any idea what would cause this? Here
is what I am doing in code:

var myWindow = null;

function show_new_win() {
var content = "some content";
if (myWindow==null) {

myWindow=window.open('','OrigFileWindow','width=1200,height=1200');

myWindow.document.write("<html><head><link type='text/css'
rel='stylesheet'
href='style.css'><script> function
mytest(id, pos){var newdiv =
document.getElementById(id);newdiv.className='highlightcnum';var
outerdiv = document.getElementById('innerfile');
outerdiv.scrollTop=pos; }<\/script></head><body >" + content + "</
body></html>");
}
myWindow.mytest("h_" + mycnum ,500);
}

Thanks for any insights.

}


Hi,

In addition to what Lee, rf and RobG wrote:

Best way to approach this loading-time problem:
1) Let the newly created window contain a onLoad handler that calls back
to the opener window.
Use: window.opener.etc to address the opener

2) After the first window (the opener) receives this call, it starts
with doing whatever it wants to do with the new window.

That way you are sure the new window is actually there.

Regards,
Erwin Moller
 
R

rob

It would be much better if content was passed to the function, e.g.

function show_new_win(content){


The faq suggests:

if (!myWindow || myWindow.closed) {

<URL:http://www.jibbering.com/faq/#FAQ4_10>




That is a very unfriendly size, use something like 400x300.




That is doomed as you don't create any elements with ids in the new
window. You should post examples that are likely to "work" with
minimal effort. At least add some resilance to the function:


innerfile isn't defined anywhere either, more to fix. Is it necessary
for the example?


It is much easier for others if you format that for posting.


mycnum isn't defined either, more for me to fix... :-(


Yes, though it's not usually a problem.


Yes again.


Don't use a "wait" (which I presume uses some guess based on
setTimeout), have a function in the child window call back to the
parent when it is ready.


Because you haven't called document.close() when you've finished with
document.write (i.e. finished writing to the document).


Yes, when you've finished writing to the child window, call its
document.close method, e.g.:

<script type="text/javascript">

var myWindow = null;

function show_new_win(content) {
var content = content || '<div>No content&hellip;</div>';
if (!myWindow || myWindow.closed) {
myWindow=window.open('','OrigFileWindow',
'width=400,height=300');

// 1200x1200 is a bit unfriendly...

myWindow.document.write(
"<html><head><link type='text/css'"
+ " rel='stylesheet' href='style.css'>"
+ "<script>"
+ "function mytest(id, pos){"
+ " var newdiv = document.getElementById(id);"
+ " if (newdiv) {"
+ " newdiv.className='highlightcnum';"
+ " }"
+ " var outerdiv = document.getElementById('innerfile');"
+ " if (outerdiv) {"
+ " outerdiv.scrollTop=pos;"
+ " }"
+ " alert('Hey, it worked');"
+ "}"
+ "window.onload = function(){opener.popWinReady();}"
+ "<\/script></head><body >"
+ content
+ "<\/body><\/html>"
);
myWindow.document.close();
}

}

function popWinReady() {
var mycnum = 10;
myWindow.mytest("h_" + mycnum ,500);

}

show_new_win('<div id="h_10">I am a div h_10</div>');

</script>

Rob,

Thanks for your help! Sorry that my example code was difficult to
work with, I appreciate you pointing out how to better provide
examples.

The callback and document.close worked wonderfully!

Thanks again.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
function wait(msecs)
{
var start = new Date().getTime();
var cur = start
while(cur - start < msecs)
{
cur = new Date().getTime();
}
}

For a shorter wait loop,

function wait(msecs) {
var end = +new Date() + msecs
while (new Date() < end) {} }

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
E

Erwin Moller

Dr said:
In comp.lang.javascript message <[email protected]


For a shorter wait loop,

function wait(msecs) {
var end = +new Date() + msecs
while (new Date() < end) {} }

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

Hi Dr JR Stockton,

A quick question:
Isn't such a loop not consuming all cpu cycles it can get?
Thus increasing the time needed to create and show a new window?

Wouldn't a window.setTimeout approach be better?

Regards,
Erwin Moller
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
, Mon, 5 Nov 2007 13:06:32, Erwin Moller <Since_humans_read_this_I_am_sp
(e-mail address removed)> posted:
Dr J R Stockton wrote:
Hi Dr JR Stockton,
Isn't such a loop not consuming all cpu cycles it can get?

Certainly - but tests on an XP SP2 system suggest that only about half
of the machine cycles are wasted. Just don't run two copies at once!
My code is shorter, and whizzes round its loop faster - that's all.
Thus increasing the time needed to create and show a new window?

Wouldn't a window.setTimeout approach be better?

It would. Though using it for the first time is IMHO harder ; perhaps
FAQ 4.20 should also have an actual example of replacing a wait loop by
using a timeout.
 
R

rf

Dr J R Stockton said:
In comp.lang.javascript message <[email protected]>
, Mon, 5 Nov 2007 13:06:32, Erwin Moller <Since_humans_read_this_I_am_sp
(e-mail address removed)> posted:



Certainly - but tests on an XP SP2 system suggest that only about half
of the machine cycles are wasted. Just don't run two copies at once!

Since the processor chips in most reasonable computers that run XP these
days use hyperthreading (that is, they appear to be two processors) I would
expect whatever monitor you use to only report 50% usage.
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top