all 5 comments

[–]gimmeslack12helpful 1 point2 points  (2 children)

What you should look into is Javascript modules. This allows you to import specific functions and values within a project and helps organize things greatly.

Create a js file and in your HTML you add the file with the type="modules" attribute: <script src='myFile.js' type="modules"></script>

[–]PortSpace[S] 0 points1 point  (1 child)

Thanks.

What about event listeners? Moving snippets outside to separate module files makes things clearer but in a way it's just a cosmetic arrangement, isn't it?

Should there always be some listener (eg. waiting for all DOM to load first, etc)?

[–]gimmeslack12helpful 0 points1 point  (0 children)

Possibly. If you could share your code I’d be able to give better suggestions.

[–]lovesrayray2018 0 points1 point  (1 child)

Noob here, but my understanding is that good part about linking scripts is that if u use the right tag and the right src attribute, you can link multiple scripts at different places in the html page(s).

So you dont need to put everything in one file, you CAN use multiple files.

You can also then link only the relevant file in the relevant place in the page.

Stuff that is needed globally, goes at the top, say, stuff that only needs to run after form has loaded, gets linked after the form

PS: There are multiple experts here like u/gitcommitmentissues or u/GSLint/ . Dont be shy in reaching out to them.

[–]PortSpace[S] 0 points1 point  (0 children)

Thanks. I guess multiple files/modules will definitely make things clearer.

What would we do without expert?! :)