+ 1

JavaScript in seperate file or in same file as html

If I want to have a weppage that uses JavaScript on my computer. Does the JavaScript has it own file or can it be in the html- file as well ?

6th Jan 2019, 1:43 PM
sneeze
sneeze - avatar
2 Answers
+ 4
I'll advice you to external js file and place the link to it just before </body> The reason I told to use external js file because the maintance is easy And the reason why I told to put the link to external js file just before </body> because there might comes situation where the script runs before the DOM content is loaded eg)//below will give error <!DOCTYPE html> <html> <head> <script> document.getElementById("para").innerHTML = "hello"; </script> </head> <body> <p id="para"></p> </body> </html> However there is another solution which does not depends on where you put script tag You can use window.onload or "DOMContentLoaded" event to run the script
6th Jan 2019, 2:20 PM
Rishi Anand
Rishi Anand - avatar