virsir
virsir

Reputation: 15509

How can I insert a script before another script loaded

I have a simple situation:

<script src="1.js"></script>
<script src="2.js"></script>

I want 1.js to load another javascript 3.js in and execute it before 2.js. I can not change the html, I can only change the 1.js code。

I tried to make a synchronize ajax call in 1.js, but still 3.js is loaded after 2.js.

Any other solution ?

Upvotes: 1

Views: 204

Answers (3)

cmwright
cmwright

Reputation: 3426

Try this:

http://www.andresvidal.com/jsl

It has lots of way to load different scripts at different times.

Upvotes: 0

RobG
RobG

Reputation: 147513

Use document.write in 1.js to add 3.js. It should be added immediately afer 1.js, but no guarantees.

Upvotes: 0

Asaph
Asaph

Reputation: 162851

Append the contents of 3.js to 1.js. If you would rather keep them separate in your code base, you can combine them in your build step. Having one less JavaScript file for the browser to load will have the pleasant side effect of reducing page load times.

Upvotes: 2

Related Questions