I am writing a website, and I've come up with quite a few scripts taking up a lot of space and ruining the aesthetics of my code. I came up with the idea of creating one script that fills a div below it with scripts, but that sounds like it could present some sort of problem. Is this an okay thing to do, or is this bad practice?
-
Sounds like a nightmare to me.Robert Harvey– Robert Harvey2016-08-04 18:33:11 +00:00Commented Aug 4, 2016 at 18:33
-
I'd just have an array of the URIs for the scripts and inject them into a div at the bottom of the page.Nick Fulton– Nick Fulton2016-08-04 18:35:46 +00:00Commented Aug 4, 2016 at 18:35
-
How about bundle all scripts into one file?Mehdi Dehghani– Mehdi Dehghani2016-08-05 15:04:42 +00:00Commented Aug 5, 2016 at 15:04
Add a comment
|
1 Answer
Use a formal convention such as require.js. For example, to load jquery-ui:
requirejs.config({
baseUrl: 'js/lib',
paths: {
// the left side is the module ID,
// the right side is the path to
// the jQuery UI file, relative to baseUrl.
// Also, the path should NOT include
// the '.js' file extension. This example
// is using jQuery UI located at
// js/lib/jquery-ui, relative to
// the HTML page.
jquery-ui: 'jquery-ui',
jquery-ui-autocomplete: 'jquery-ui-autocomplete'
}
});
and later on using the autocomplete plugin:
require([ "jquery-ui-autocomplete" ], function( autocomplete ) {
autocomplete({ source: [ "One", "Two", "Three" ] }, "<input>" )
.element
.appendTo( "body" );
});
References