Firefox's inline JS execution order

9 views
Skip to first unread message

JoJo

unread,
Dec 15, 2009, 3:57:32 PM12/15/09
to Prototype & script.aculo.us
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?

Alex McAuley

unread,
Dec 16, 2009, 9:16:44 AM12/16/09
to prototype-s...@googlegroups.com
Why not attach the widget code to dom:ready....

Alex Mcauley
http://www.thevacancymarket.com
> --
>
> 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-s...@googlegroups.com.
> To unsubscribe from this group, send email to
> prototype-scripta...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/prototype-scriptaculous?hl=en.
>
>
>

JoJo

unread,
Dec 17, 2009, 7:26:40 PM12/17/09
to Prototype & script.aculo.us
How does dom:ready prevent multiple executions of the same script?

On Dec 16, 6:16 am, "Alex McAuley" <webmas...@thecarmarketplace.com>
wrote:


> Why not attach the widget code to dom:ready....
>

> Alex Mcauleyhttp://www.thevacancymarket.com

T.J. Crowder

unread,
Dec 18, 2009, 2:50:37 AM12/18/09
to Prototype & script.aculo.us
Hi,

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 `
$A`?

HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com

JoJo

unread,
Dec 18, 2009, 3:14:26 PM12/18/09
to Prototype & script.aculo.us
I can't use any of Prototype's load listeners or defer because this is
before Prototype has been loaded (chicken and egg dilemma). The
"God" (straight up JS) needs to make sure that the egg (Prototype) and
chicken (my code that uses Prototype) are only created once. There is
no other JS that I am including that could conflict with this
bootstrapper.

As a simple test, I only included this code once in the body tag:

if (typeof(RAN_ONCE) === 'undefined') {
RAN_ONCE = true;
// minified Prototype code here
// minified my widget code here
}

It succeeds in IE but fails in FF with the "$A undefined" error. Now
I know it is not a matter of Prototype being redefined. It fails with
just running once!

Reply all
Reply to author
Forward
0 new messages