Re: Document loaded:how to detect?



On Jul 12, 5:43 pm, "D.Campagna" <ynnadre...@xxxxxxxxxxxxx> wrote:
Peter Michaux ha scritto:> On Jul 10, 5:23 am, "D.Campagna" <ynnadre...@xxxxxxxxxxxxx> wrote:

Isn't there an easier way to detect if a stupid thing like the full load
of a page ?

You can add a "load" listener to the iframe using addEventListener (or
attachEvent in IE.) This load event is non-standard but seems to be
widely supported. I just asked about this same issue recently

http://groups.google.com/group/comp.lang.javascript/browse_frm/thread...

Peter

Many thanks. (BTW I must thank you for the very interesting things I
have read in your articles about feature detection etc. too. I'm
definitely one of your fans.)

Glad you liked the feature detection, etc. articles. Did you read the
fine print?

I am new to javascript but in another
language I am absolutely not a beginner, so i am confident one day or
another I'll be able to write some decent scripts, with a little help
from you gurus. :-)

However, this seems to imply you consider my posts here to be
trolling.

http://groups.google.com/group/comp.lang.javascript/msg/777a72b794a461f1

Doesn't reconcile. You should take your time in drawing such
conclusions. If not for my "trolling", there would have been no
feature detection (actually feature testing) article(s).


So far, I continued to test since my post, and tried to manage the
differences betwen IE and FF. With IE and Opera, I am pretty comfortable
with readyState, it seems to work (but at present I tested only in IE7
and Opera 9.24).

That doesn't sound comforting to me. Why do you feel the need to use
the readyState property?


For FF it's a completely different and messy story. If readyState is not
available for the Iframe (as is in FF 3.1x), I tried to inject a script
in the Iframe: (here is "myIframe")

var myvar=false;
var head=myIframe.contentWindow.document.getElemetByTagName("head").item(0);
var script=myIframe.contentWindow.document.createElement("script");
script.type="text/javascript";
script.textContent="function init(){parent.myvar=true}";
head.appendChild(script);
myIframe.onload=myIframe.contentWindow.init();

There's a typo in the second line and you are making a lot of bad
assumptions, particularly about script injection.


That causes the Iframe to execute the newly created init() when loaded
and it seems to work pretty good. If myvar is false I use a timer to
repeat the test and when it's true I am sure the Iframe is fully loaded
(as suggested by Randy Webb IIRC, even if he was talking about something
else)

Yes, I'm sure he was not talking about this. And you seem to have it
backwards. Why would you need a timer? Just call whatever it is you
need to call on load (as opposed to a function that sets a watched
flag.)


Now anyway I incurred in another problem, because the init function is
NOT so easy as in my previous example, it's a recursive one that tries
to detect the frame (this script in my 'test case' should be injected
from any frame, not the "parent" of the Iframe) and it seems that FF has
some problems with an injected function that calls himself (to scan
top.frames and find a frame called "cmdframe", the one that contains the
script and the variable to set). I discovered BTW that
IE does not support script.textContent.

Yes, read Randy's posts on script injection again.


What a mess!

Not really. You are making it much harder than it is. Perhaps this
is not the best problem for you to solve at this stage.


I will try the suggestions found in the thread you gave me. (ouf!)

I omit comments about the mess javascript appears to be and the
headaches it's causing to me, and appreciate your input.

Javascript is *not* a mess. Never has been. Your current failings
are related to your point on the learning curve and *DOM* variations
(which have nothing to do with the language.)
.



Relevant Pages