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?

3
  • Sounds like a nightmare to me. Commented 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. Commented Aug 4, 2016 at 18:35
  • How about bundle all scripts into one file? Commented Aug 5, 2016 at 15:04

1 Answer 1

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 and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.