无法在页面中使用两次JavaScript

时间:2014-11-19 21:14:17

标签: javascript function timer scroll

我是一个我没有创建的网站的新管理员(专业人员,我只是一个以JavaScript开头的IT学生)。我想在网站的主页上添加一个计时器,其中包含我在互联网上创建的以下工作代码:

<script language="JavaScript1.2"> 
//##################################################
// Author: ricocheting.com 
// For: public release (freeware) 
// Date: 4/24/2003 (update: 6/26/2009) 
// Description: displays the amount of time until the "dateFuture" entered below. 


// NOTE: the month entered must be one less than current month. ie; 0=January, 11=December 
// NOTE: the hour is in 24 hour format. 0=12am, 15=3pm etc 
// format: dateFuture = new Date(year,month-1,day,hour,min,sec) 
// example: dateFuture = new Date(2003,03,26,14,15,00) = April 26, 2003 - 2:15:00 pm 

dateFuture = new Date(2014,10,25,20,00,00); 

// TESTING: comment out the line below to print out the "dateFuture" for testing purposes 
//document.write(dateFuture +"<br />"); 


//################################### 
//nothing beyond this point 
function GetCount(){ 

dateNow = new Date();   //grab current date 
amount = dateFuture.getTime() - dateNow.getTime();  //calc milliseconds between dates 
delete dateNow; 
adversaire = "Saastal";

// time is already past 
if(amount < 0){ 
document.getElementById('countbox').innerHTML="Le match Sion - " + adversaire + " a débuté !"; 
} 
// date is still good 
else{ 
days=0;hours=0;mins=0;secs=0;out=""; 

amount = Math.floor(amount/1000);//kill the "milliseconds" so just secs 

days=Math.floor(amount/86400);//days 
amount=amount%86400; 

hours=Math.floor(amount/3600);//hours 
amount=amount%3600; 

mins=Math.floor(amount/60);//minutes 
amount=amount%60; 

secs=Math.floor(amount);//seconds 

if(days != 0){out += days +" jour"+((days!=1)?"s":"")+", ";} 
if(days != 0 || hours != 0){out += hours +" heure"+((hours!=1)?"s":"")+", ";} 
if(days != 0 || hours != 0 || mins != 0){out += mins +" minute"+((mins!=1)?"s":"")+", ";} 
out += secs +" seconde"+((secs!=1)?"s":""); 
document.getElementById('countbox').innerHTML="Temps restant avant Sion - " + adversaire + " : " + out; 

setTimeout("GetCount()", 1000); 
} 
} 

window.onload=GetCount;//call when everything has loaded 

</script> 
<div id="countbox"></div>

唯一的问题是,当我添加此代码(有效)时,页面上已有的另一个JavaScript代码(滚动文本)不再起作用。这是滚动文本的代码,但重要的是我用&#34;右键单击/查看页面源&#34;创建了它。并且我无法更改它,除了文本部分(在管理页面中,我有一个文本框,我在其中编写要滚动的文本,并根据以下代码,此文本只是JavaScript函数的可变部分):

<h3 class="replace">Agenda</h3>
<script language="JavaScript1.2">
    //  Distributed by http://www.hypergurl.com
    // Scrollers width here (in pixels)
    var scrollerwidth="180px";
    // Scrollers height here
    var scrollerheight="100px";
    // Scrollers speed here (larger is faster 1-10)
    var scrollerspeed=1;
    /* Scrollers content goes here! Keep all of the message on the same line!
     * var scrollercontent='<font face="Arial" color="green" size="5">
     * <b>Place your content here.<br> 
     * vous pouvez inclure des balises HTML, des hyperliens 
     * Script distributed by <a href="http://www.hypergurl.com">Hypergurl.com.</a> 
     * The scrolling massage will now pause on mouseover.<br>
     * Thanks David for the update!</b></font>'
     * le texte de la marquee doit être inclu dans une balise <div> ... </div>
     * ATTENTION: les aphostrophes doivent impérativement être échappés!!!!
     */
    var txt = ' '

+&#39;

这是我可以写的文字

&#39;         var scrollercontent =&#39;&#39;         + txt         +&#39;&#39 ;;         var pauseit = 1;

    // Change nothing below!

    scrollerspeed=(document.all)? scrollerspeed : Math.max(1, scrollerspeed-1) //slow speed down by 1 for NS
    var copyspeed=scrollerspeed
    var iedom=document.all||document.getElementById
    var actualheight=''
    var cross_scroller, ns_scroller
    var pausespeed=(pauseit==0)? copyspeed: 0

    function populate(){
    if (iedom){
    cross_scroller=document.getElementById? document.getElementById("iescroller") : document.all.iescroller
    cross_scroller.style.top=parseInt(scrollerheight)+8+"px"
    cross_scroller.innerHTML=scrollercontent
    actualheight=cross_scroller.offsetHeight
    }
    else if (document.layers){
    ns_scroller=document.ns_scroller.document.ns_scroller2
    ns_scroller.top=parseInt(scrollerheight)+8
    ns_scroller.document.write(scrollercontent)
    ns_scroller.document.close()
    actualheight=ns_scroller.document.height
    }
    lefttime=setInterval("scrollscroller()",50)
    }
    window.onload=populate

    function scrollscroller(){

    if (iedom){
    if (parseInt(cross_scroller.style.top)>(actualheight*(-1)+8))
    cross_scroller.style.top=parseInt(cross_scroller.style.top)-copyspeed+"px"
    else
    cross_scroller.style.top=parseInt(scrollerheight)+8+"px"
    }
    else if (document.layers){
    if (ns_scroller.top>(actualheight*(-1)+8))
    ns_scroller.top-=copyspeed
    else
    ns_scroller.top=parseInt(scrollerheight)+8
    }
    }

    if (iedom||document.layers){
    with (document){
    if (iedom){
    write('<div style="position:relative;width:'+scrollerwidth+';height:'+scrollerheight+';overflow:hidden" onMouseover="copyspeed=pausespeed" onMouseout="copyspeed=scrollerspeed">')
    write('<div id="iescroller" style="position:absolute;left:0px;top:0px;width:100%;">')
    write('</div></div>')
    }
    else if (document.layers){
    write('<ilayer width='+scrollerwidth+' height='+scrollerheight+' name="ns_scroller">')
    write('<layer name="ns_scroller2" width='+scrollerwidth+' height='+scrollerheight+' left=0 top=0 onMouseover="copyspeed=pausespeed" onMouseout="copyspeed=scrollerspeed"></layer>')
    write('</ilayer>')
    }
    }
    }

所以我的问题是:你知道让这两个JavaScript函数在同一页面上工作的方法吗?我只是想让我的计时器在主页上,或者在滚动文本中(也在主页上,在右栏中)...

提前感谢您的帮助。

种类问候, user3507737

3 个答案:

答案 0 :(得分:2)

您将一个动作分配给window.onload事件两次。在javascript的第一个块中,您有window.onload=GetCount;,在第二个块中,您有window.onload=populate(顺便说一下,最后需要一个分号)。 您只能为onload事件分配一个函数,因此最好创建一个调用GetCount并填充的函数,并将此新函数分配给window.onload。查看更多in this answer

答案 1 :(得分:2)

您似乎通过包含第二个脚本来覆盖第一个脚本的window.onload来电,该脚本有自己的.onload电话。

我将从上面的脚本中删除window.onload开头的两行,并添加第三个​​&lt; script&gt;在您的页面中执行以下操作的标记:

window.onload = function () {
    GetCount();
    populate();
};

这应该让你的脚本都运行。

答案 2 :(得分:1)

您需要删除绑定到onload的当前代码,并将其替换为:

window.onload = function () {
    populate();
    GetCount();
};