P
P
Peter2015-11-02 17:48:24
JavaScript
Peter, 2015-11-02 17:48:24

How to load multiple js files after a condition is met?

About ten or even more js files need to be loaded into the project after the condition is met.
Option via

var script=document.createElement('script');
script.src='http://hostjs-mybb2011.narod.ru/js/alert_tst.js';
document.body.appendChild(script);

does not fit, because, if I understand correctly, for each file you will have to prescribe such a thing.
What are other options for solving my problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Wolf, 2015-11-02
@volkov_p_v

register(['a.js', 'b.js', 'etc.js']);

function register(urls) {
  urls.forEach(function(url) {
    var script=document.createElement('script');
    script.src = url;
    document.body.appendChild(script);
  });
}

R
Rafael™, 2015-11-02
@maxminimus

In such cases, they usually make a subroutine that takes a list of urls as input. The
subroutine bypasses the list of urls and creates script elements.
This thing will be written once

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question