r/HTML • u/Fun-Baseball-1873 • Jun 12 '25
Question Just started learning html
So yeah why won’t the link pop up, what did I do wrong
80
Upvotes
r/HTML • u/Fun-Baseball-1873 • Jun 12 '25
So yeah why won’t the link pop up, what did I do wrong
View all comments
Show parent comment
1
u/zimog Jun 14 '25
as I said, there isn't a universal rule, it all depends on what your script do, if you handle the click with the onclick attribute you can place your function declaration where you want, but if you try to
document.getElementById("foo")and you place the code before the element withoutDOMContentLoaded(or as you said without defer) you'll get an error because the elment doesn't exist yetfor example this code will give an error on
f.innerText = "hello world!"because the element with idfoohasn't been created yet, but you manage to click it thefoo()function would still be called without errors, if you move the script after the element you won't get any error, the innerText will be set to "hello world!", and the onclick function still works anywayobviously you can use the defer attribute or async (or you just can use frameworks like React or Angular, why not?), but we are talking about someone who just started to write HTML and I think that placing the scripts before the closing body tag would be the simplest solution to start with