[haXe] [js] being sure all is loaded before proecessing
Franco Ponticelli
franco.ponticelli at gmail.com
Mon Apr 3 21:58:47 CEST 2006
No, because you can't be always sure that the DOM tree is built before the
Main method is processed. If your js is small enough and in cache it should
be theoretically parsed before the </body> starts the parsing of the DOM.
This also prevents problems in case you want to include other JS files (or
CSS) which maybe complete their loading after the main method has been
fired.
Another good reason is to put the <script> tag in the <head>, which is in my
opinion the right place.
2006/4/3, Benjamin Dasnois <benjamin.dasnois at gmail.com>:
>
> I can't understand why this may create problems if you want to
> manipulate the DOM?! Simply put the js file just before the </body>
> tag, no?
>
> On 4/3/06, Franco Ponticelli <franco.ponticelli at gmail.com> wrote:
> > Actually when you include you js (haxe generated), the main method is
> called
> > on the end of the js parsing.
> > This may create problems if your script pretends to manipulate the DOM
> of
> > the page.
> > To get around this I do the following:
> > <code>
> > class MainClass
> > {
> > static public function main() : Void
> > {
> > if(js.Lib.document.body == null)
> > js.Lib.window.onload = onload;
> > else
> > onload(null);
> > }
> >
> > static public function onload(evt : Event) : Void
> > {
> > js.Lib.alert("hello");
> > }
> > }
> > </code>
> >
> > All my code goes in onload.
> > Pay attention that I have had to add the event onload in the Window
> class
> > (which is legal).
> >
> > This way you can include the js in the head instead of directly in the
> body
> > ...
> >
> > Hope someone will find this useful :-)
> >
> > --
> > haXe - an open source web programming language
> > http://haxe.org
> >
> >
>
>
> --
> DASNOIS Benjamin
>
> --
> haXe - an open source web programming language
> http://haxe.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.motion-twin.com/pipermail/haxe/attachments/20060403/6fb48e32/attachment.htm
More information about the Haxe
mailing list