/    Sign up×
Community /Pin to ProfileBookmark

how: js file include other js

hello guys i have almost 100 jsp/html files

that it use 3-4 js files, each of them has more of 2000 lines of code

i want split code of these 3-4 js files to the necessary new files maybe (10 .js)

my problem is how a js file can [b]include[/b] other js,
i dont want lose my functions references

thanks in advanced

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@TJ111Mar 03.2008 — You can just create a new <script> tag via the DOM and insert it into the head of the document. Ie:
<i>
</i>var newScript = document.createElement("script");
newScript.src="/path/to/js";
//you can even test when it has finished loading via the readyState ([i]I think[/i])
newScript.onreadystatechange = function() {
if (newScript.readyState == 4) {
someFunction("in newScript.js");
}
}
Copy linkTweet thisAlerts:
@gil_davisMar 03.2008 — my problem is how a js file can include other js,[/quote]
You can't.
i dont want lose my functions references[/quote]
You won't. You just have to list (instantiate) all the files in the web page.

So, instead of:
<i>
</i>&lt;script ... src="myjs.js"&gt;&lt;/script&gt;

you would have:
<i>
</i>&lt;script ... src="myjspt1.js"&gt;&lt;/script&gt;
&lt;script ... src="myjspt2.js"&gt;&lt;/script&gt;
&lt;script ... src="myjspt3.js"&gt;&lt;/script&gt;
&lt;script ... src="myjspt4.js"&gt;&lt;/script&gt;
Copy linkTweet thisAlerts:
@ZeroKilledMar 03.2008 — i often use this function which load external script into my page. it simple, you pass as many url js file as necessary;
<i>
</i>function LoadScript(){
var source = Array.apply(null, arguments);
var script = document.createElement('script');
script.setAttribute('src', source.shift());
script.setAttribute('type', 'text/javascript');
document.getElementsByTagName('head')[0].appendChild(script);
if(source.length)arguments.callee.apply(null, source);
}

//example:
LoadScript('/myjs.js', 'http://www.site.com/js/file.js', ...);




---
ooops, one after another...
Copy linkTweet thisAlerts:
@seatoskyhkApr 25.2008 — Hi. I have tried the code.. (and also other script loading method). but I dunno why but none of this work.

In both firefox and IE, I got a complain saying that the variable 'test02' doesn't exist. Why? did I do anything wrong?


I had my own script before that 'appendChild" of a script elemnt to the head, but also doesn';t work.

I see so many people use it and some how they works but it just doesn't work on mine.. >_<~~~

----------------------- js/test02.js ----------------

var test02 = "abcde";

alert (test02);

------------------------------- HMTL -----------------

<html>

<head>

</head>

<body>

<script language="JavaScript" type="text/javascript">

function LoadScript(){
var source = Array.apply(null, arguments);
var script = document.createElement('script');
script.setAttribute('src', source.shift());
script.setAttribute('type', 'text/javascript');
document.getElementsByTagName('head')[0].appendChild(script);
if(source.length)arguments.callee.apply(null, source);
}


LoadScript('js/test02.js');

</script>
<script language="JavaScript">


alert("Hmmm" + " : " + test02);
</script>


</body>

</html>
Copy linkTweet thisAlerts:
@felgallApr 25.2008 — You don't need to use Ajax to pull additional JavaScript from the server. You just need to add a script tag to your page. See http://javascript.about.com/library/bladdjs.htm for info on how to add a script tag to your page using JavaScript.
Copy linkTweet thisAlerts:
@ZeroKilledApr 25.2008 — greeting people, had been a long time i don't come back...

for [B]seatoskyhk[/B]:

it worked for me on msie7 and firefox. i had double checked my script and your script and i don't see anything wrong. i guess you may need to provide more detailed info. or upload your code to the web so we can check your code and hopefully help you.
Copy linkTweet thisAlerts:
@seatoskyhkApr 25.2008 — haha. i have just posted it to another thread.. b/c they are interrelated.

Please see my two scripts here:

http://www.murvinlai.com/tech/json/

Let me know if you spot anything wrong.. ?
×

Success!

Help @dr_pompeii spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...