Separate – deceze Jun 13 '17 at 09:05

  • https://stackoverflow.com/questions/29918246/javascript-inline-vs-external-script-whats-the-difference –  Jun 13 '17 at 09:06
  • 2 Answers2

    1

    A script element can either load an external file or contain code, it can't do both. So to do both, at least two script elements are required.

    So:

    <script src="lib/calculator.js"></script>
    

    loads a file, and

    <script>
        calculator.init();
    </script>
    

    runs some code. If the code in the second element was included as content of the first, like:

    <script src="lib/calculator.js">
        calculator.init();
    </script>
    

    the external file will be loaded but the element content (i.e. calculator.init()) will be ignored.

    RobG
    • 142,382
    • 31
    • 172
    • 209
    0

    In the first script tag <script type="text/javascript" src="lib/calculator.js"></script>, you are including the file which contains all code for the calculator.

    In order to actually use the calculator functions, you need to (depending on the code) either instantiate it or, as you do here, initialize it. This you're doing in an inline code block, so hence, the <script> calculator.init(); </script>. Note that you could actually put this (and your other own code) in another external file and import that in the same way.

    Remy Kabel
    • 946
    • 1
    • 7
    • 15