Optimizing a JavaScript Site for Search Engines - Functionality and Optimization
(Page 5 of 9 )
Functionality and Optimization
So how do you combine the benefits of optimization with the value and functionality of JavaScript? There are a few things you can do to improve your overall SEO value of your site, even with JavaScript.
1. Off-load your JavaScript
The best way to optimize a page with JavaScript is to move the code to an external file, then simply point to it in your HTML. Let’s compare some code. The following example is a web page with JavaScript to display the current date, as you saw in Figure-1. The first example shows the code directly in the HTML header.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>My Website</title>
<script language="javascript" type="text/javascript">
<!--
function GetDay(nDay)
{
var Days = new Array("Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday");
return Days[nDay]
}
function GetMonth(nMonth)
{
var Months = new Array("January","February","March","April","May","June",
"July","August","September","October","November","December");
return Months[nMonth]
}
function DateString()
{
var Today = new Date();
var suffix = "th";
switch (Today.getDate())
{
case 1:
case 21:
case 31:
suffix = "st"; break;
case 2:
case 22:
suffix = "nd"; break;
case 3:
case 23:
suffix = "rd"; break;
};
var strDate = GetMonth(Today.getMonth())+ " " + Today.getDate()+ ", " ;
if (Today.getYear() < 2000)
{
strDate = strDate + (Today.getYear() +1900);
}
else
{
strDate=strDate + Today.getYear();
}
return strDate;
}
document.write(DateString());
//--></script>
</head>
<body>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis ligula lorem, consequat eget, tristique nec, auctor quis, purus. Vivamus ut sem. </p>
</body>
</html>
Example 1: JavaScript Date Function directly in HTML
Placing this code in the HTML of your web page not only takes up a lot of space, it forces the search engine spider to wade through many lines of code, affecting your text to code ratio, your keyword density, and your content positioning.
Next: Move the Code >>
More Search Optimization Articles
More By Jennifer Sullivan Cassidy