Script tags are executed in strict document order on all browsers
(unless the `defer` attribute is put on the script tag), not least
because the scripts might output to the document inline via
`document.write` -- and so order of execution is critical. (The
`defer` attribute changes that by telling the browser that the script
won't be outputting to the document and so the browser can continue
formatting and parsing without executing the script, if it wants.) But
I'm guessing you're not using `defer`, so things will occur in
document order.

Regarding running in parallel, JavaScript in browsers is single-
threaded (by design), so you can be certain your script is the only
one running on the page at the moment it's running.

You can't *reliably* use code that accesses the DOM (`$$`) inline; you
need to go looking for things once the DOM is fully-populated. That's
why Prototype supplies the dom:loaded event, although there are other
ways, including window.onload (but that waits until images are
complete), putting the call to trigger things in a script right at the
end of the document before the closing <html> tag (and even then, I'd
use a Function#defer to let the browser really finish, but I'm a bit
paranoid), etc.

But forget the DOM for a moment, I can't see any reason why `$A` would
be undefined as long as the code using it appears in a script loaded
after Prototype. But I'm not sure I follow you about Prototype getting
"redefined" -- is it being included more than once? I'm pretty sure
that doesn't work (in fact, we just had a thread here in the group
about a specific way that didn't work on IE7).

If Prototype isn't being loaded more than once, are there other third-
party scripts or libraries on the page that may be interfering with `

T.J. Crowder
Independent Software Consultant
tj / crowder software / com

On Dec 15, 8:57 pm, JoJo <tokyot...@gmail.com> wrote:
> I'm creating a widget where I tell people to copy and paste this code
> onto their site:
>    <script type="text/javscript" src="http://www.site.com/script.js";></
> script>
>    <div class="widget"></div>
> Script.js looks for all the widget divs on the page by using $$ and
> dynamically fills them up.  Now, I'm not catering to nerd programmers
> who know that it's better to do this when they need to embed multiple
> widgets:
>    <head>
>       <script>
>    </head>
>    <body>
>       <div class="widget"></div>
>       <div class="widget"></div>
>       <div class="widget"></div>
>    </body>
> Instead, I'm expecting stupid people to be doing this:
>    <body>
>       <script>
>       <div class="widget"></div>
>       <script>
>       <div class="widget"></div>
>       <script>
>       <div class="widget"></div>
>    </body>
> So I've tried to only execute my script once using:
>    if (typeof(RAN_ONCE) === 'undefined') {
>       RAN_ONCE = true;
>       // minified Prototype code here
>       // minified my widget code here
>    }
> This code works well in IE, but not in FF. FF always reports an error
> saying $A is undefined. I'm guessing that FF actually ran my code in
> parellel and thus Prototype got redefined and screwed up?  Does anyone
> know how execution order is different in IE and FF with inline JS?


You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to