"INCLUDES" question -- html and/or JS

What, if any, is the difference between putting an include in JS, as opposed to in html, which is preferable (and why), and are here any good or bad consequences on doing so in BOTH at the same time ?

ie HTML
<script type="text/html" import-script="/JS/coherent.js"></script>

JS
Include.addScript("/JS/dataStorage.js");

From ChatGPT:

Including scripts in JavaScript and HTML serves different purposes and fits different scenarios in web development. Let’s break down the differences and implications:

Including in HTML

Including a script in HTML is the traditional and most common way to load JavaScript files into a web page. You use the <script> tag to reference the JavaScript file, and the browser will load and execute it as it parses the HTML. Here’s a typical example:

<script src="/JS/coherent.js"></script>

Pros:

  • Browsers are optimized to download and parse scripts included this way.
  • Scripts are loaded as the HTML document is parsed, allowing you to control the order of script execution by the placement of the <script> tags.
  • You can include scripts asynchronously or defer their loading by using the async or defer attributes to prevent blocking the rendering of the page.

Cons:

  • If not managed properly (for example, without async or defer), it can lead to render-blocking, where the browser must pause parsing the HTML to fetch and execute the script.
  • It’s less dynamic. You cannot conditionally load a script based on user interactions or other runtime conditions (without adding more JavaScript).

Including in JavaScript

When you include a script via JavaScript, you’re often looking to dynamically load scripts based on certain conditions or events. This can be done using different techniques, such as using Include.addScript (which is not a standard JavaScript method but could be part of a library or a custom utility function).

Pros:

  • You can conditionally load scripts, which can improve performance by only loading scripts when they’re needed.
  • It allows for more dynamic and complex application behaviors, such as loading modules on demand.

Cons:

  • If not done correctly, it can lead to complexities with script execution order, which can cause dependencies to break.
  • It can be more complex to manage and track which scripts have been loaded in larger applications.

Using Both Methods

Using both methods at the same time isn’t typically necessary and can lead to several issues:

  • Redundancy: If both methods try to load the same script, you might end up downloading and executing the same code twice, which is wasteful and can cause unexpected behavior if the script isn’t idempotent (designed to be run multiple times without different effects).
  • Maintainability: It can make your codebase harder to maintain, as it might be unclear to other developers why a script is included twice and in different ways.
  • Performance: There could be performance implications, such as increased load times and memory usage if both methods are not well coordinated.

In practice, you should choose one method of including scripts based on the requirements of your project. For static script loading where you know the dependencies upfront, using HTML is simple and effective. For more complex, dynamic applications where scripts need to be loaded based on runtime conditions, including scripts with JavaScript may be the better choice.

Finally, there is a modern approach using JavaScript modules with import statements that can also dynamically load modules as needed and might be a more elegant solution for complex applications. This method is highly recommended for modern web development as it provides clear benefits in terms of scope control, namespace management, and tree shaking during the build process.

Thanks @runshotgun – I had not considered using ChatGPT.

Your question might not have includ3d MSFS, as I am still left wondering what method is best fro MSFS from a performance point of view, or if it makes any difference for the MSFS Application,

Using BOTH seems to open up issues, and while still seems to work, probably should be avoided…

MSFS uses a version of Chrome dating back to 2016.

Since it behaves in accordance with web standards of the time, it should behave the same as a regular browser would back in those days.

In have to wonder if MSFS 2024 will still be using a 2016 version of Chrome, or what the consequences would be to changing to a more up-to-date version ?

Last I heard is that the newer versions of Coherent aren’t as flexible as the version we currently have.

Also, upgrading to something else might break the years of work that have been done on gauges and JS modules.

100% understand and agree.

If it ain’t broke, dont fix it.

Unfortunately, the typical engineers mentality is

“If it ain’t broke, take it apart and change it … then it’s broke, and you can then TRY to fix it”

1 Like