0

right now in my footer I have 2 .js files ( jquery.min.js - myjs.js ).

So, by considering that myjs.js is a very small file 5kb can I copy this code into the jquery.min.js file to reduce 1 request?

What is better bewtween: Leave 2 files, inline myjs.js or merge this 2 files?

Thank you

3
  • 1
    of course you can Commented Mar 12, 2018 at 23:17
  • 2
    Going forward, new versions of jQuery will be released on a schedule that has nothing to do with your own application. Combining the files might be something better done during a build process. Commented Mar 12, 2018 at 23:20
  • Reminder that if you decide to combine the two files, remember that order matters. If myjs.js uses jQuery then jQuery needs to be included first. Commented Mar 12, 2018 at 23:21

2 Answers 2

1

As said, you can add your code to the jQuery file, however I would not recommend doing so.

Having two separate files allows you to more easily expand, debug, and modify your own code. It would also help others looking at your site (as well as you future self) understand what's going on; alljs.js is opaque compared to appjs.js and jQuery.js. Additionally, updating jQuery in future, should you want to, is much easier if jQuery isn't mixed in with your own code. And having two separate files allows the browser to cache them independently.

If you would like to only have one JS file, including your small Javascript inline is a better option. However, I would much sooner recommend other optimizations, like setting up a build process to minimize your JS and deploy it to a dedicated production server.

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, provided that you host the file you can modify it however you want to - after all it's basically a text file with a file .js extension. Simply open it with a text editor (or your IDE), and add your JavaScript to the bottom. Don't delete their licensing or comments though out of respect for property rights.

Better to serve 1 file for performance purposes. The establishment of the TCP link (the pipe) to fetch the second file is not insignificant. Another option is to put that other javascript in the HTML file, in a <script> tag just before the close of your <body> tag.

2 Comments

I don't know if you saw it, but I added answer detailing some reasons to keep the code separated into two files. Do you mind elaborating on why you would recommend serving one file?
@Matthias I updated answer to address your comment. Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.