Inline Javascript Best Practices for SEO

When creating a website, there are two important considerations – making your pages as user friendly as possible and making those pages easily read by search engines. It can be difficult to accommodate both concepts, especially if you are looking to make your pages interactive in some way, or if you are looking to make your pages attractive and as user friendly as possible. There are two technologies currently in use that are, arguably, good for users, but not so good for search engines. The technologies are Flash and JavaScript.

The problem with both is that search engines find it difficult to read either of these unless they are written and used with a search engine in mind. SEO best practices for JavaScript is to use it only when really necessary, and to use it as an external source rather than inline. When used externally, JavaScript files are stored in a similar way to CSS files, with routines called when required.

JavaScript has one annoying feature – it tends to slow page load times, especially if you have used it frequently inline. When reading your page, a search engine will constantly come up against road blocks because of its inability to accurately interpret that JavaScript. For readers, pages will pause as each inline script is read and acted upon. This can be annoying for visitors to your site.

So what is the difference between ‘inline’ and ‘external’ JavaScript code? Inline JavaScript has the complete routine written each time it is used. External JavaScript is a file that contains the code required to perform an action, and that code is called as and when its required. For example, if you have mouse-over routines for your menus, you only need to have that code called when a user places the mouse over the menu item. Inline programming will read that code immediately, external code will only read it when a mouse-over occurs.

For best practices, once you have written your external code, you will need to include a reference to it on your web page. Where CSS code is referenced in the header section of your page’s code, JavaScript can be left until after the most important data – your content. If you place the reference to the JavaScript code just before the command, browsers (and search engines), will read your code and publish your content quickly, leaving the loading of the JavaScript code until the very end. This helps to speed up load times and helps to provide a much better user (and search engine) experience.

The key to successfully using SEO-friendly JavaScript is to write your code in such a way that browsers with Java disabled are still able to function properly, and this includes areas where JavaScript has been used. Menus are a good example. They can be written in HTML and so work in any browser. JavaScript can be added then to add any interactions required. Search engines don’t need to worry about interactions, they are only interested in the menu data and the links. Used correctly, you gain the benefits of JavaScript to improve a user’s experience whilst not undermining your SEO efforts.

Author: admin

Leave a Reply