Forum Moderators: open
I'm using some java script on my site that is held in the <head> tags which allows a visitor to add my site to their favourites.
I.e
<head>
<script type="text/javascript">
**script here**
</script>
</head>
I'm now using a sophos virus info feed but this is placed within the <body></body> of my html.
My problem is that when I validate my page using the W3C validator, the javascript makes it fail all over the place.
How do I go about adding this script to my <head> section? I don't know much at all about Javascript so sorry if it's not possible/a dumb question!
-----------------------------------------------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>My title</TITLE>
<META name='description' content='My description'>
<META name="keywords" content="my keywords">
<LINK REL="SHORTCUT ICON" href="http://www.mywebsite.com/favicon.ico">
<LINK REL="stylesheet" TYPE="text/css" HREF="/style/style.css" TITLE="style">
<script type="text/javascript">
function bookmarksite(title, url){
if (document.all)
window.external.AddFavorite(url, title);
else if (window.sidebar)
window.sidebar.addPanel(title, url, "")
}
</script>
</HEAD>
<BODY>
<!-- *********** Sophos info feed: latest virus alerts - START *********** -->
<script language="JavaScript1.1"
src="http://www.sophos.com/virusinfo/infofeed/tenalerts.js"></script>
<script type="text/javascript" language="JavaScript1.1">
<!--
// Want to customise the look and feel of the virus information
// on your site? Simply change the following values to your own
// preferences.
//
// Changing the value of 'count' will change the number of rows
// displayed.
// Changing the value of 'target' to 'new' will display the link in
// a new window.
// *****************************************************************
var bgcolour_main = '#F9F9FB'
var bgcolour_heading = '#FFFFFF'
var text_colour = '#361D61'
var link_colour = '#AD4B80'
var font_size = '1'
var font_face = 'verdana, arial, helvetica, sans-serif'
var count = 10 // number should be between 1 and 10
var first_column = 'yes' // 'yes' or 'no'
var cellspacing = 'yes' // 'yes' or 'no'
var target = '_new' // '_self' or 'new'
// *****************************************************************
// Be careful not to change anything below this line!
var htmltxt = "";
var font_desc = '<font face="' + font_face + '" size="'
+ font_size + '" color="' + text_colour + '">';
var tmp_count = 0;
if (first_column == "no") {
colspan = 1;
} else {
colspan = 2;
}
if (cellspacing == "no") {
cellspacing = 0;
} else {
cellspacing = 2;
}
if (count > 0) {
if (count > 10) {count = 10}
htmltxt += '\n<table width=100% cellpadding="6" cellspacing="'
+ cellspacing + '" border="0" bgcolor="#ffffff" align="center">';
if (count == 1) {
htmltxt += '\n<tr><td colspan="' + colspan + '" bgcolor="'
+ bgcolour_heading + '">' + font_desc
+ '<b>Latest virus alert</b></font></td></tr>';
} else {
htmltxt += '\n<tr><td colspan="' + colspan + '" bgcolor="'
+ bgcolour_heading + '">' + font_desc + '<div align="center"><u><b>Latest '
+ count + ' virus alerts</b></u></div></font></td></tr>';
}
for (var i=0; i<tenalerts.length; i+=3) {
tmp_count++;
if (tmp_count > count) {
break;
} else {
if (first_column == "no") {
htmltxt += '\n<tr><td bgcolor="' + bgcolour_main + '">'
+ font_desc + '<a href="' + tenalerts[i+2] + '" target="'
+ target + '"><font color="'
+ link_colour + '"><nobr>' + tenalerts[i+1]
+ '</nobr></font></a></font></td></tr>';
} else {
htmltxt += '\n<tr><td bgcolor="' + bgcolour_main + '">'
+ font_desc + '<nobr>' + tenalerts[i+0]
+ '</nobr></font></td><td bgcolor="' + bgcolour_main + '">'
+ font_desc + '<a href="' + tenalerts[i+2] + '" target="'
+ target + '"><font color="'
+ link_colour + '"><nobr>' + tenalerts[i+1]
+ '</nobr></font></a></font></td></tr>';
}
}
}
} else {
document.write
("=== Sophos info feed error: Specify value of count > 0 ===");
}
// Please do not remove the code after this point.
htmltxt += '\n<tr><td colspan="' + colspan + '" bgcolor="'
+ bgcolour_heading + '"><center>' + font_desc
+ 'Source: <a href="http://www.sophos.com" target="'
+ target + '"><font color="' + link_colour
+ '">Sophos</font></a></center></td></tr>';
htmltxt += '\n<tr><td colspan="' + colspan + '" bgcolor="'
+ bgcolour_heading + '"><center>' + font_desc
+ '<a href="http://www.sophos.com/virusinfo/infofeed/" target="'
+ target + '"><font color="' + link_colour
+ '">Add this info to your website</font></a></center></td></tr>';
htmltxt += '\n</table>';
document.write(htmltxt);
//-->
</script>
<!-- *********** Sophos info feed: latest virus alerts - END *********** -->
</BODY>
</HTML>
-----------------------------------------------------------------------------------------------
W3C Validation Errors are as follows...
- end tag for element "FONT" which is not open
- end tag for element "B" which is not open.
- end tag for "SCRIPT" omitted, but its declaration does not permit this
- end tag for "CENTER" omitted, but its declaration does not permit this.
- character "'" is not allowed in the value of attribute "COLSPAN".
- value of attribute "COLSPAN" must be a single token.
- element "TENALERTS.LENGTH" undefined.
Many of these errors are repeated multiple times.
If I added the code to the <head></head> section, what do I do with the code thats already there? Also, if I move the code out of the body tags, how do I execute the script from within the html tags?
Many thanks
You just need to cut & paste the code from the <body></body> section to the <head></head> section.
what you need to do is:
- cut all the code from the body and insert a div element (<div id="sophosInfo"></div>) there
- paste everything into a function that you place in the <head>
- instead of document.write, you should put that output in a variable (for example 'var output')
- at the end of the function you put the contents of the output var into the div (document.getElementById('sophosInfo;).innerHTML= output;)
- activate that function with an onLoad in the <body> tag
And apart from that you need to fix the HTML the script generates.
htmltxt += '\n<tr><td colspan="' + colspan + '" bgcolor="'
+ bgcolour_heading + '">' + font_desc
+ '<b>Latest virus alert</b></font></td></tr>';
Should be:
htmltxt += '\n<tr><td colspan="' + colspan + '" bgcolor="'
+ bgcolour_heading + '">' + font_desc
+ '<b>Latest virus alert<\/b><\/font><\/td><\/tr>';
Fix that and your validation errors might go away (at least one of them will... the closing font one).