I heard that putting the javascript in the html code isn't good for SEO and that it's better to put it in a .js file. It's clear that it makes the code more understandable but is it the only reason?
Other reasons to keep JavaScript in external files:
Note that using JavaScript should make little difference to SEO (externally or internally) unless:
In the case of 1, it's best not to create links with client-side JavaScript when you can. In the case of 2, it's good practice to minify stylesheets and JavaScript, then combine files to serve it with as few HTTP requests as possible. |
|||
If you include the JavaScript in the HTML, then the user has to download the JavaScript for every page they view that needs it. If you make it an external file, then they have to download it once, which is slightly slower than including it in the page, because of the extra HTTP request. However, after that, it will be cached and they don't need to download it again. In terms of the impact on SEO, that's the benefit - your site is quicker, which Google rewards greatly. This means that if you have a piece of JavaScript that a user is going to need repeatedly, it should be external. If it's a one-off, you should include it on the page to reduce the number of HTTP requests. Examples of where on-page JavaScript could be beneficial:
For code management, you are still best to develop with the code in an external file and then use a deployment script to grab the code and insert it for the production version of your code. Finally, shared libraries like jQuery should always be external, because they will often already be cached in a visitor's browser from another site they visited. |
|||
|
There are two additional reasons for external JS files.
|
|||
|