Question

Can anyone tell me WHY these two javascripts won't cooperate on the same page together?

Script #1

This goes in the head:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript">
//<![CDATA[
<!--
var tWidth='600px';                  // width (in pixels)
var tHeight='18px';                  // height (in pixels)
var moStop=true;                     // pause on mouseover (true or false)
var fontfamily = 'arial,sans-serif'; // font for content
var tSpeed=7;                        // scroll speed (1 = slow, 5 = fast)

// enter your ticker content here (use \/ and \' in place of / and ' respectively)
var content='<a href="http:\/\/www.url.com\/">7679 - Reset Passwords<\/a>, ';


var cps=tSpeed; var aw, mq; var fsz = parseInt(tHeight) - 4; function startticker(){if (document.getElementById) {var tick = '<div style="position:relative;width:'+tWidth+';height:'+tHeight+';overflow:hidden;"'; if (moStop) tick += ' onmouseover="cps=0" onmouseout="cps=tSpeed"'; tick +='><div id="mq" style="position:absolute;left:0px;top:0px;font-family:'+fontfamily+';font-size:'+fsz+'px;white-space:nowrap;"><\/div><\/div>'; document.getElementById('ticker').innerHTML = tick; mq = document.getElementById("mq"); mq.style.left=(parseInt(tWidth)+10)+"px"; mq.innerHTML='<span id="tx">'+content+'<\/span>'; aw = document.getElementById("tx").offsetWidth; lefttime=setInterval("scrollticker()",50);}} function scrollticker(){mq.style.left = (parseInt(mq.style.left)>(-10 - aw)) ?parseInt(mq.style.left)-cps+"px" : parseInt(tWidth)+10+"px";} window.onload=startticker;
//-->
//]]>
</script>

This goes in the body for script #1:

<div id="ticker" align="center">
</div>

The above script marquees text RTOL (right to left) across the screen (marquee). Stops marquee on mouseover. Handy little script, but the problem is it breaks when I implement the next script to the page...

Script #2

This goes in the head:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript">
//<![CDATA[
<!--

function show2(){
if (!document.all&&!document.getElementById)
return
thelement=document.getElementById? document.getElementById("tick"): document.all.tick
var Digital=new Date()
var hours=Digital.getHours()
var minutes=Digital.getMinutes()
var seconds=Digital.getSeconds()
var dn="PM"
if (hours<12)
dn="AM"
if (hours>12)
hours=hours-12
if (hours==0)
hours=12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
var ctime=hours+":"+minutes+":"+seconds+" "+dn
thelement.innerHTML=""+ctime+""
setTimeout("show2()",1000)
}
window.onload=show2
//-->
//]]>
</script>

This goes in the body for script #2:

<span id="tick"></span>

The above script just displays the current machine time, working as a clock, counts hours, minutes, and seconds. When I add this script to the page, my above script #1 (marquee) disappears from the page. Any suggestions?

Was it helpful?

Solution

Both scripts are using window.onload = something, so the second script is preventing the first from initializing.

In the first, remove the window.onload, and change the second to:

function loadBothScripts()
{
    startticker();
    show2();
}

window.onload = loadBothScripts;

OTHER TIPS

You have this line in your first script:

window.onload = startticker;

And then you have this in the second script:

window.onload = show2;

The second one over-writes the window.onload event so the first script does not fire. You can use this function to attach multiple handlers to one event:

// addEventListener fix for IE
// o: object to which event listener is assigned
// e: event name as string
// f: the function
function myAddEventListener(o, e, f) {
    if (o.addEventListener) { // standards compliant browsers
        o.addEventListener(e, f, false);
    } else if (o.attachEvent) { // IE8 and earlier
        o.attachEvent("on" + e, f);
    }
}

// replace all window.load = someFunction statements with this:
myAddEventListener(window, "load", startticker);
myAddEventListener(window, "load", show);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top