Monday 5 December 2016

import - Using a JavaScript file in another JavaScript file




I am writing some JavaScript files that will not be used with HTML documents. For example, writing a calculator in one JavaScript file, where I'll have different .js files say one for addition, subtraction, multiplication, division, etc..




I'd like to have each math operation in a self contained .js file then have another .js file that will #include the other smaller files and can call the functions from them?



Is that possible?


Answer



Using javascript:



var script = document.createElement('script');
script.src = '/js/script';
document.head.appendChild(script);



Using jQuery:



//you need to change your path
$.getScript('/js/script.js', function()
{
// script is imported

});


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...