1

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 Aug 4 '16 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 Aug 4 '16 at 18:35
  • How about bundle all scripts into one file? – Mehdi Dehghani Aug 5 '16 at 15:04
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.