I discovered why the marquee was buggy under IE when using a DOCTYPE. The problem was that it could not determine its own internal positioning properly if a DOCTYPE and the <center> tag were both used. This would also be true of using any type of centering 'container' tags. I've made a fix (added an alignment variable so that tags are not needed to center it) and a number of other enhancements, most of which are transparent.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title>Optional Reverse, Alternate, Percentage Width, Controls and Height Adjust Marquee - Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<script type="text/javascript">
/* NOTES: If your page uses a DOCTYPE, DO NOT use
<center></center> or any type of centering 'container' tags
around the marquee. This will cause problems in IE. Use the
'alignmarquee' variable in the below configuration instead.
ALSO: If using the percentage width feature, the page will reload
onresize. DO NOT use this feature if your page also contains
content that could be lost on reload like, forms data. */
/* Cross browser Marquee script- © Dynamic Drive (www.dynamicdrive.com)
For full source code, 100's more DHTML scripts, and Terms Of Use,
visit http://www.dynamicdrive.com This Credit MUST stay Intact.
Reverse, controls, percentage width, alternate & Height Adjustment
courtesy of jscheuer1 in http://www.dynamicdrive.com/forums */
//Use Controls? Set to 0 for no, 1 for yes
var controls=1
//Set Height Adjustment to center marquee vertically within its cell
var hgtAdj=3
//Specify the marquee's width in pixels or percentage (see above note)
var marqueewidth="80%"
//Specify the marquee's height in pixels
var marqueeheight="25px"
//Align marquee - use 'left' 'center' or 'right'
var alignmarquee='center'
//Specify the marquee's speed (whole numbers, larger is faster 1-10)
var marqueespeed=2
//configure background color:
var marqueebgcolor="#DEFDD9"
//Pause marquee onMousever (0=no, 1=yes)?
var pauseit=1
//Set initial direction 'forward' or 'reverse'
var direction='reverse'
//Set direction to alternate (0 for no, 1 for yes)
var alternate=0
//Specify the marquee's content below. Keep all content on ONE line.
// Backslash any single quote (') (ie: that\'s great):
var marqueecontent='<font face="Arial">Thank you for visiting <a href="http://www.dynamicdrive.com">Dynamic Drive.</a> If you find this script useful, please consider linking to us by <a href="../link.htm">click here.</a> Enjoy your stay!</font>'
////NO NEED TO EDIT BELOW THIS LINE////////////
marqueecontent='<nobr>'+marqueecontent+'</nobr>'
function altM(){
direction=(direction=='reverse')? 'forward' : 'reverse'
}
var ie=document.all && !window.opera
function iecompattest(){
return (document.compatMode && document.compatMode.indexOf("CSS")!=-1)? document.documentElement : document.body
}
if (((typeof(window.onresize))==('object')||('undefined'))&&(marqueewidth.indexOf('%')!==-1))
window.onresize=refresh;
function refresh() {
setTimeout("doit()",1)
}
function doit(){
if (window.location.reload)
window.location.reload( false );
else if (window.location.replace)
window.location.replace(unescape(location.href))
else
window.location.href=unescape(location.href)
}
if (marqueewidth.indexOf('%')!==-1)
var marqueewidth=ie? (iecompattest().clientWidth*parseInt(marqueewidth)/100)-20+'px' : (window.innerWidth*parseInt(marqueewidth)/100)-20+'px'
var copyspeed=marqueespeed
var pausespeed=(pauseit==0)? copyspeed: 0
var iedom=document.all||document.getElementById
var actualwidth=''
var cross_marquee, ns_marquee
function populate(){
if (iedom){
cross_marquee=document.getElementById? document.getElementById("iemarquee") : document.all.iemarquee
cross_marquee.innerHTML=marqueecontent
actualwidth=cross_marquee.firstChild.offsetWidth
if (direction=='reverse')
cross_marquee.style.left=8-actualwidth+"px"
else
cross_marquee.style.left=parseInt(marqueewidth)+8+"px"
}
else if (document.layers){
ns_marquee=document.ns_marquee.document.ns_marquee2
ns_marquee.left=parseInt(marqueewidth)+8
ns_marquee.document.write(marqueecontent)
ns_marquee.document.close()
actualwidth=ns_marquee.document.width
}
lefttime=setInterval("scrollmarquee()",20)
}
window.onload=populate
function scrollmarquee(){
if (iedom){
var c1=(direction=='reverse')? (parseInt(cross_marquee.style.left)<parseInt(marqueewidth)) : (parseInt(cross_marquee.style.left)>(actualwidth*(-1)-18));
if (c1)
(direction=='reverse')? cross_marquee.style.left=parseInt(cross_marquee.style.left)+copyspeed+"px" : cross_marquee.style.left=parseInt(cross_marquee.style.left)-copyspeed+"px"
else{
if (alternate)
altM();
(direction=='reverse')? cross_marquee.style.left=8-actualwidth+"px" : cross_marquee.style.left=parseInt(marqueewidth)+8+"px"
}
}
else if (document.layers){
var c1=(direction=='reverse')? (ns_marquee.left<parseInt(marqueewidth)) : (ns_marquee.left>(actualwidth*(-1)-18))
if (c1)
(direction=='reverse')? ns_marquee.left+=copyspeed : ns_marquee.left-=copyspeed
else{
if (alternate)
altM();
(direction=='reverse')? ns_marquee.left=8-actualwidth : ns_marquee.left=parseInt(marqueewidth)+8
}
}
}
if (iedom||document.layers){
with (document){
write('<table border="0" cellspacing="0" cellpadding="0" align="'+alignmarquee+'"><td>')
if (controls)
write('<span style="cursor:pointer;" onmouseover="direction=\'forward\';copyspeed++" onmouseout="copyspeed--"><< </span></td><td>')
if (iedom){
write('<div style="position:relative;width:'+marqueewidth+';height:'+marqueeheight+';overflow:hidden">')
write('<div style="position:absolute;width:'+marqueewidth+';height:'+marqueeheight+';background-color:'+marqueebgcolor+'" onMouseover="copyspeed=pausespeed" onMouseout="copyspeed=marqueespeed">')
write('<div id="iemarquee" style="position:absolute;left:0px;top:'+hgtAdj+'px"></div>')
write('</div></div>')
}
else if (document.layers){
write('<ilayer width='+marqueewidth+' height='+marqueeheight+' name="ns_marquee" bgColor='+marqueebgcolor+'>')
write('<layer name="ns_marquee2" left=0 top='+hgtAdj+' onMouseover="copyspeed=pausespeed" onMouseout="copyspeed=marqueespeed"></layer>')
write('</ilayer>')
}
write('</td>')
if (controls)
write('<td><span style="cursor:pointer;" onmouseover="direction=\'reverse\';copyspeed++" onmouseout="copyspeed--"> >></span></td>')
write('</table>')
}
}
</script>
</body>
</html>
Bookmarks