New Web Dev here.
Recently I got into DOM manipulation with JS, everything was sweet with one-page websites, but then I tried with projects with many html files and that's when I got confused.
It's a static website, each page has different elements so my first thought was "Ok so, one JS file per page, right?" And I think that's fine for a 2-3pages websites, but what if it is a medium size one? I mean 10-15 pages (home, login, profile page, settings, a gallery page with a carousel, etc). The browser would make so many requests.
I googled for a while and I read people who claim they 'merge' their JS files into one single script so I tried it. I don't know if I misunderstood something but I literally put all my JavaScript into one file, now I have a big JS file and a lot of errors on the console (obviously) because the script can't find a 'register' button on the home page, a 'profile-foto' in the login form and a 'carousel' on the settings page.
So here are my questions:
What do they mean by 'merging' scripts?
Is there a tool for that? How does it work?
What do you do guys in these cases?
Thanks in advance!
document.querySelectorAll('.my-class')
, it will try to go through the document and find those elements. However, if you place that code in a function (or in an object), then when the script is executed, it'll just create the function (or the object), 1/2