You can put your JavaScript code anywhere in the webpage. As we know that the loading of the website starts from the top of the webpage and excites and presents the user one by one. So, you can put it anywhere but let me clear one thing with a scenario.
Suppose, you have created a web page and you have added a large library on the <head> tag at the top of the webpage. So, how will the webpage load. First, it will start loading from above and it will switch to the first script tag. If user's internet speed will be slow then library will take some time and without fully loading that library nothing will render on user machine and one more thing without website content there is no need of javascript for user . So, through this what we have understood is to load the UI of the website first and then after switching to JavaScript. This is the best way to make your website good for your user.
Hope this information has cleared your confusion.
Happy Coding!
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
You can put your JavaScript code anywhere in the webpage. As we know that the loading of the website starts from the top of the webpage and excites and presents the user one by one. So, you can put it anywhere but let me clear one thing with a scenario.
Suppose, you have created a web page and you have added a large library on the <head> tag at the top of the webpage. So, how will the webpage load. First, it will start loading from above and it will switch to the first script tag. If user's internet speed will be slow then library will take some time and without fully loading that library nothing will render on user machine and one more thing without website content there is no need of javascript for user . So, through this what we have understood is to load the UI of the website first and then after switching to JavaScript. This is the best way to make your website good for your user.
Hope this information has cleared your confusion.
Happy Coding!