i have a script tag on the index.html page so it links the app.js to the index.html
i also have the same script tag on the gallery.html
the app.js file has code that changes the dom in both html files. is this the correct way to link the app.js file to the html pages, because I am getting javascript errors when I try to do something on the gallery.html page, and I think it is because the app.js file is expecting to find some the html elements from the index.html page but cannot so does not know what to do. hopefully that made sense
Can you post your code? Specifically, can you post your script tags?
It could be that the link to the app.js file isnât correct. Maybe itâs in a different folder. But this is just an assumption, as I donât have too much to go off of. If you can, give us the specific errors youâre getting and describe your file tree to us.
code is like this on the gallery page , the errors I am getting are things like
TypeError: Cannot set property âtextContentâ of null which refers to this part of the js file
let htmlDate = document.getElementById(âdateâ);
htmlDate.textContent = date;
but this works fine when Inspect the page when I am on index.html, so thats why to me it seems like when the app.js runs on gallery.html , it expects to find some of of the elements that are taken from the index.html page because I have things like, document.getElementById(âdkfâ) which refers to things on the index.html page, but are not on the gallery.html page, is that normal?
it is on the index.html page, and works fine on that page, thats kind of what I mean, the js has a bunch of references to elements that are on the index.html page so when it runs on the gallery.html page it doesnt seem to to know that for example date refers to something on the index.html page
If you have different pages with different elements, but only one JS file with all the code then you are bound to get such errors.
You can load different scripts per page, or as suggested, you can make sure you always check the elements before using them (mind the quotes in the copy-pasted code BTW, they should be '' or "").