1

Im a bit clules on the following situation, i am building a site, and right now all my js functions are in one js file, yes i know its a very bad idea.

What i am unfamiliar with, when i was checking other sites, i saw they include jQuery in the header and in the footer, different scripts are loaded.

I am unfamiliar, and please be nice i am a bit of a beginner. Do people use some kind of plugin for this? or they include every script manualy in the specified file at the buttom?

I would like to break the scripts to parts, and not to include everything in one file. What i mean by this, some functions only required in the profile page, some in the settings, and some in the login.

If someone could please give me some info about this would really help me.

Thank you

Side
  • 1,753
  • 9
  • 35
  • 64
  • Having all your js in one file isn't ALWAYS bad. The file will be cached at the client and you will save many round trips from the client to the server. – Steve Wellens Feb 02 '13 at 15:29

2 Answers2

1

That's the sort of thing that one would use RequireJS for.

http://requirejs.org/docs/start.html

It will allow you to setup dependencies for your different JS files. These dependencies would then be loaded as needed.

jeremysawesome
  • 7,033
  • 5
  • 33
  • 37
0

Do people use some kind of plugin for this? or they include every script manualy in the specified file at the buttom?

Well, usually websites are made in PHP from individual components. Think of it as bricks. Some of these bricks contain JS code. When they are put together you end up with some pages having unique parts of javascript code in different places. It's not particularly good, but can dramatically simplify and speed up development.

I would like to break the scripts to parts, and not to include everything in one file.

Usually you want to include your JS with <script> tags in website header. If for some reason you'd like to dynamically include external JS then you may try to use this simple function: include();

i am building a site, and right now all my js functions are in one js file, yes i know its a very bad idea.

It's bad from development point of view, but when it comes to deploying the website it's a very good habit as it can decrease loading time of your website (and the loading time is one of most essential things). There are even applications compiling multiple .js files into a single minified file in order to get best performance.

MarcinWolny
  • 1,600
  • 2
  • 27
  • 40
  • I wouldn't say that compiling the JS files into one single minified file is really all that beneficial. The download hit for your Javascript files really only matters for the first page load. After that the file will remain cached for a while, and so you won't pay any extra penalties for it. Say you do, however, decide to combine all your JS into one minified file and then have to change one semi-colon in one tiny portion of it. Now you pay the download price for the whole file again for that one little fix... – jeremysawesome Feb 02 '13 at 15:39
  • Same can be said about loading multiple images, and yet: CSS sprites are one of most recommended practices. Same thing with multiple JS and multiple CSS. Only thing you should keep separate are libraries that you can pull from CDNs, like for example: always use jQuery form Google CDN instead of hosting it yourself. So long story short: It's one of the key things to minimize number of files people have to download. As well as their size. And "tiny bug fixes" have minimal impact on overall user experience comparing to downloading of 8 or more separate JS files. – MarcinWolny Feb 02 '13 at 15:44
  • I'll say that it depends on your situation. In some cases it's beneficial to combine all JS into one file, in some cases it can be not beneficial. This might be a good compromise: http://stackoverflow.com/a/4878038/296889 – jeremysawesome Feb 02 '13 at 16:12
  • Ok, question then: When it's not beneficial to combine JS files while deploying a completed website or web application? Cause as I said - besides external libraries on CDNs - I don't see any circumstances where you'd benefit from having 8 files instead of 1 on deployed website (again: NOT a development version). – MarcinWolny Feb 02 '13 at 16:15