Distribute the jQuery code in several .js

0

With reference to this question:

How to include a JavaScript file to another Javascript file without using JQuery?

I have a js of very large jQuery code, so, having everything a little more orderly, it occurred to me to create several js and load them all in a js that would be the one called in the index.

I imagine that the load will always be greater than if everything is in a single file. But not always what counts is the burden if not having the most polished and ordered code. As long as it is something that is acceptable and that does not really affect the page's load.

Having said that, what is the best option? load those scripts with jQuery or use pure JavaScript. In terms of performance. Thanks

Greetings

    
asked by Isaac Palacio 28.10.2018 в 13:33
source

2 answers

1

In terms of performance: VanillaJS is better, in fact after the last version of ECMAScript that came out, programming in JQuery has almost no sense, since JSvanilla has greatly improved its syntax to make it more "friendly". If you want my advice, use Vanilla if you work alone, put a lot less obstacles and limits when developing. If it is to find a job, learn jQuery, but learn it when you already dominate vanilla upside down and right.

To improve performance, you should use code minifiers, such as Webpack, Gulp, Laravel-Mix, etc.

And about "how to save them" depends on the platform you use. The vast majority stores in the assets folder the code dependencies of the front, and they are divided by each view, then in the controller that loads the dependencies of each view you are telling them which modules to load. The same with CSS. It does not make sense to load the complete CDQ of jQuery for the whole page, although it is a common practice nowadays, it is optimal to define each route the load of elements that correspond to it.

    
answered by 28.10.2018 / 15:26
source
1

You could do something like this:

let nuevoScript = document.createElement('script');
nuevoScript.setAttribute("src", "el/otro/script.js");
document.body.appendChild(nuevoScript);

As regards the performance of having only one script, things seem to have changed. Please open css-tricks.com in view-source and see how many script elements you have.

This is a link to a similar question asked in Stack Overflow in English: link

    
answered by 28.10.2018 в 14:57