Jump to content

Can an external JS file require another JS file?


omar199
 Share

Recommended Posts

I have one HTML page that is requiring a JS file with a lot of code in it. I was thinking about splitting up a part of that JS file into another JS file. Does anyone know if there is a way for the first JS file (called by the HTML file) can "require" or "include" the new JS file as well?(I'm trying to do this without modifying the HTML)

Link to comment
Share on other sites

Does anyone know if there is a way for the first JS file (called by the HTML file) can "require" or "include" the new JS file as well?

Put this in your first JS file, where x.js is the other included file
document.write('<script src="x.js"></script>');

Link to comment
Share on other sites

You could use a serverside script, like php:script1.php:document.write("Hello");<?php include "script2.js" ?>document.write("World!");That way the script2.js file would be loaded on the server into the script1.php file, and you could still call the file in html as a text/javascript file I think.

Link to comment
Share on other sites

Thanks for all the help everyone.I think that I will take the first method that Dan suggested, simply modifying the HTML files. I'll just use homesite or dreamweaver to run a global find and replace to change the

<script src=file1></script>

to include both (change that to

<script src=file1></script><script src=file2></script>

Link to comment
Share on other sites

But thee point is, wouldn't that slow down the loading time? because when you load both at the html document, they get loaded simultaniously wink.gif Elseway, wel, not simultaniously tongue.gif
If you have a server or a connection so slow that you can actually tell a difference between the loading times of those, then you should shoot yourself anyway, and the problem is immediatley solved. :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...