Rob
Rob

Reputation: 2786

How to run a javascript script before the other ones?

Do you know if it is possible to make sure that a JavaScript script site is ran first in a web page?

For the context, I'm trying to tag every span before another library (just like cufon) kicks in and messes with the original content. Since it's not possible to use onLoad() on spans, it's the only way I can see to do that on the client side.

Upvotes: 5

Views: 4933

Answers (2)

Scott Stevens
Scott Stevens

Reputation: 2651

Order of precedence is determined by the order in which things load.

  1. Head tag scripts (in the order they are listed)
  2. Scripts & other event attributes in the order they appear in the code.
  3. Body onLoad

I did have #2 & #3 mixed up - to clarify the body onLoad event fires after the entire body (scripts included) loads

Also, scripts altering <span> tags will have no effect if placed in the head tag (unless they are a function) since the script runs when it is loaded, and when the head loads, the body & span tags haven't loaded yet.

Upvotes: 6

Oleg V. Volkov
Oleg V. Volkov

Reputation: 22471

Place <script> tag in page source after content that you need to modify, but before any other scripts. Scripts run in same order that their tags appear on page.

Upvotes: 1

Related Questions