If I understand correctly, your program loads HTML and scripts dynamically for every page you open. It should be something like a Single Page Application .
Well, the first step is to understand the strategies you can take to load pages.
Load everything at the beginning
Depending on the size and usage of the system, one strategy may be to concatenate all HTMLs in one and all Scripts in a single script.
The final size will be larger, but once loaded, the system will have a much faster response time.
If this strategy is feasible, you just need to make sure that the script initialization is done once.
Load on demand
This is the strategy you are adopting now. Each page accessed loads its own HTML and JavaScript.
The error you are making, however, is loading the HTML and JavaScript with each access. You should ensure that they are reused on subsequent hits.
In the case of Script is easy. Instead of manually adding to the DOM, create a function in the main script where pages may require a specific script.
For example, a very rudimentary implementation follows:
var scripts = {};
function require(pagina, src) {
if (src in scripts) {
//carregar script de "src"
scripts[src] = true;
}
}
Use AMD
Better yet, you can use AMD and get rid of duplicate code. There, module script management will be fully managed by a trusted library and currently following the web standard.
Note that AMD is a standard that can have different implementations.
You can make each page an AMD module, for example:
define('myModule', ['dep1', 'dep2'], function (dep1, dep2) {
//Define the module value by returning a value.
return function () {};
});
So whenever you need to access a module simply do so:
var $ = require('jQuery');
var myModule = require('myModule');
The AMD manager ensures that the module will be downloaded and initialized only once.
End dynamic pages
Maybe your HTML is rendering on the server. For responsive application, it would be far better to leave static HTML templates and bind bind from data obtained via Ajax calls to a REST endpoint.
Advantages include:
- Better performance
- Least amount of data trafficked
- More consistent system API
- Avoid another layer of technology to generate dynamic pages, which always general some kind of problem
- Scalability greatly facilitated
The downside is that you need to be very careful not to end up with too much complicated code in JavaScript.
Beware of events
Placing event handlers directly on elements, even more on pages that can load multiple times is always a problem.
An alternative is not to bind directly to the element, but to properly use the on
function of jQuery. Example:
$('#main-content').on('click', '#btn-salvar', function() {...})
The 'live' function used to add events to elements that were not already in the DOM, but now the form above is used.
The idea is that the event will fire whenever any button with id=btn-salvar
is clicked inside an element with id=main-content
. Regardless of whether the button exists, it will be created or recreated within that element.