PDA

View Full Version : Script won't allow page to validate



harambeegirl
08-22-2006, 04:36 PM
1) Script Title: Switch Content Script

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex17/switchcontent.htm

3) Describe problem: I put the Switch Content Script on my page and it works fine. However, when I enter this URL into the W3C Markup Validation Service at http://www.validator.w3.org/ it does not validate.

Below, I have copied the errors that the validator recorded and the code that created the errors. Please let me know what I can do to get my page to validate.



This page is not Valid XHTML 1.0 Transitional!
Below are the results of checking this document for XML well-formedness and validity.

Error Line 23 column 91: required attribute "alt" not specified.
...r image, use: <img src="whatever.gif">
The attribute given above is required for an element that you've used, but you have omitted it. For instance, in most HTML and XHTML document types the "type" attribute is required on the "script" element and the "alt" attribute is required for the "img" element.

Typical values for type are type="text/css" for <style> and type="text/javascript" for <script>.

Error Line 23 column 91: document type does not allow element "img" here.
...r image, use: <img src="whatever.gif">
The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).

One common cause for this error is the use of XHTML syntax in HTML documents. Due to HTML's rules of implicitly closed elements, this error can create cascading effects. For instance, using XHTML's "self-closing" tags for "meta" and "link" in the "head" section of a HTML document may cause the parser to infer the end of the "head" section and the beginning of the "body" section (where "link" and "meta" are not allowed; hence the reported error).

Error Line 23 column 92: end tag for "img" omitted, but OMITTAG NO was specified.
... image, use: <img src="whatever.gif">
You may have neglected to close an element, or perhaps you meant to "self-close" an element, that is, ending it with "/>" instead of ">".

Info Line 23 column 68: start tag was here.
...'- ' //HTML for contract symbol. For image, use: <img src="whatever.gif">
Error Line 28 column 38: document type does not allow element "style" here.
document.write('<style type="text/css">')

Error Line 37 column 22: character ";" not allowed in attribute specification list.
for (i=0; i<rootlength; i++){

Error Line 37 column 22: element "rootlength" undefined.
for (i=0; i<rootlength; i++){
You have used the element named above in your document, but the document type you are using does not define an element of that name. This error is often caused by:

incorrect use of the "Strict" document type with a document that uses frames (e.g. you must use the "Frameset" document type to get the "<frameset>" element),
by using vendor proprietary extensions such as "<spacer>" or "<marquee>" (this is usually fixed by using CSS to achieve the desired effect instead).
by using upper-case tags in XHTML (in XHTML attributes and elements must be all lower-case.

Error Line 84 column 39: character ";" not allowed in attribute specification list.
for (i=0; i<selectedComponents.length-1; i++)

Error Line 84 column 39: element "selectedComponents.length-1" undefined.
for (i=0; i<selectedComponents.length-1; i++)

Warning Line 139 column 24: character "&" is the first character of a delimiter but occurred as data.
if (enablepersist=="on" && ccollect.length>0){
This message may appear in several cases:

You tried to include the "<" character in your page: you should escape it as "&lt;"
You used an unescaped ampersand "&": this may be valid in some contexts, but it is recommended to use "&amp;", which is always safe.
Another possibility is that you forgot to close quotes in a previous tag.

Warning Line 139 column 25: character "&" is the first character of a delimiter but occurred as data.
if (enablepersist=="on" && ccollect.length>0){

Warning Line 145 column 22: character "&" is the first character of a delimiter but occurred as data.
if (ccollect.length>0 && statecollect.length>0)

Warning Line 145 column 23: character "&" is the first character of a delimiter but occurred as data.
if (ccollect.length>0 && statecollect.length>0)

Warning Line 156 column 24: character "&" is the first character of a delimiter but occurred as data.
if (enablepersist=="on" && document.getElementById)

Warning Line 156 column 25: character "&" is the first character of a delimiter but occurred as data.
if (enablepersist=="on" && document.getElementById)

Error Line 159 column 8: end tag for "selectedComponents.length-1" omitted, but OMITTAG NO was specified.
</script>

Info Line 84 column 11: start tag was here.
for (i=0; i<selectedComponents.length-1; i++)
Error Line 159 column 8: end tag for "rootlength" omitted, but OMITTAG NO was specified.
</script>

Info Line 37 column 11: start tag was here.
for (i=0; i<rootlength; i++){


I have also pasted the code that had the errors.



<script type="text/javascript">

/***********************************************
* Switch Content script- Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use. Last updated April 2nd, 2005.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var enablepersist="on" //Enable saving state of content structure using session cookies? (on/off)
var collapseprevious="no" //Collapse previously open content when opening present? (yes/no)

var contractsymbol='- ' //HTML for contract symbol. For image, use: <img src="whatever.gif">
var expandsymbol='+ ' //HTML for expand symbol.


if (document.getElementById){
document.write('<style type="text/css">')
document.write('.switchcontent{display:none;}')
document.write('</style>')
}

function getElementbyClass(rootobj, classname){
var temparray=new Array()
var inc=0
var rootlength=rootobj.length
for (i=0; i<rootlength; i++){
if (rootobj[i].className==classname)
temparray[inc++]=rootobj[i]
}
return temparray
}

function sweeptoggle(ec){
var thestate=(ec=="expand")? "block" : "none"
var inc=0
while (ccollect[inc]){
ccollect[inc].style.display=thestate
inc++
}
revivestatus()
}


function contractcontent(omit){
var inc=0
while (ccollect[inc]){
if (ccollect[inc].id!=omit)
ccollect[inc].style.display="none"
inc++
}
}

function expandcontent(curobj, cid){
var spantags=curobj.getElementsByTagName("SPAN")
var showstateobj=getElementbyClass(spantags, "showstate")
if (ccollect.length>0){
if (collapseprevious=="yes")
contractcontent(cid)
document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="block")? "block" : "none"
if (showstateobj.length>0){ //if "showstate" span exists in header
if (collapseprevious=="no")
showstateobj[0].innerHTML=(document.getElementById(cid).style.display=="block")? contractsymbol : expandsymbol
else
revivestatus()
}
}
}

function revivecontent(){
contractcontent("omitnothing")
selectedItem=getselectedItem()
selectedComponents=selectedItem.split("|")
for (i=0; i<selectedComponents.length-1; i++)
document.getElementById(selectedComponents[i]).style.display="block"
}

function revivestatus(){
var inc=0
while (statecollect[inc]){
if (ccollect[inc].style.display=="block")
statecollect[inc].innerHTML=contractsymbol
else
statecollect[inc].innerHTML=expandsymbol
inc++
}
}

function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function getselectedItem(){
if (get_cookie(window.location.pathname) != ""){
selectedItem=get_cookie(window.location.pathname)
return selectedItem
}
else
return ""
}

function saveswitchstate(){
var inc=0, selectedItem=""
while (ccollect[inc]){
if (ccollect[inc].style.display=="block")
selectedItem+=ccollect[inc].id+"|"
inc++
}

document.cookie=window.location.pathname+"="+selectedItem
}

function do_onload(){
uniqueidn=window.location.pathname+"firsttimeload"
var alltags=document.all? document.all : document.getElementsByTagName("*")
ccollect=getElementbyClass(alltags, "switchcontent")
statecollect=getElementbyClass(alltags, "showstate")
if (enablepersist=="on" && ccollect.length>0){
document.cookie=(get_cookie(uniqueidn)=="")? uniqueidn+"=1" : uniqueidn+"=0"
firsttimeload=(get_cookie(uniqueidn)==1)? 1 : 0 //check if this is 1st page load
if (!firsttimeload)
revivecontent()
}
if (ccollect.length>0 && statecollect.length>0)
revivestatus()
}

if (window.addEventListener)
window.addEventListener("load", do_onload, false)
else if (window.attachEvent)
window.attachEvent("onload", do_onload)
else if (document.getElementById)
window.onload=do_onload

if (enablepersist=="on" && document.getElementById)
window.onunload=saveswitchstate

</script>

DimX
08-22-2006, 04:43 PM
Make your script tags look as follows:


<script type="text/javascript">
// <![CDATA[

...

// ]]>
</script>

mwinter
08-22-2006, 04:57 PM
[Use CDATA sections]

The far more sensible solution is to place the script in an external file.

Mike

DimX
08-22-2006, 05:00 PM
The far more sensible solution is to place the script in an external file.
Yes, it is actually.