javascript - 不可能在一个页面中使用两次 JavaScript

标签 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 代码(滚动文本)不再有效。这是滚动文本的代码,但重要的是我用“右键单击/查看页面源代码”创建了它,除了文本部分(在管理页面中,我有一个文本框,我无法更改它)编写要滚动的文本,根据以下代码,该文本只是 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 = ' '

+ '

这是我可以写的文字

' var 滚动内容 = '' + 文本 + ''; var暂停=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 函数在同一页面上工作吗?我只想让我的计时器在主页上,或者在滚动文本中(也在主页上的右栏中)...

预先感谢您的帮助。

谨致问候, 用户3507737

最佳答案

看来您通过包含第二个脚本来覆盖第一个脚本的 window.onload 调用,第二个脚本有自己的 .onload 调用。

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

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

这应该会让您的两个脚本都运行。

关于javascript - 不可能在一个页面中使用两次 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27027107/

相关文章:

c++ - 使用 CreateTimerQueueTimer、Visual Studio 2012、C++ 创建计时器,定期运行

javascript - setTimeout 对于长时间调度作业是否安全?

javascript - 如何缩小 liquid 模板生成器生成的 javascript 文件

r - r编程中的拉格朗日插值公式

c++ - 使用模板函数传递模板参数时出错

r - R中的日期时间差异

javascript - 在 javascript eslint 中允许使用分号

javascript - $scope 如何在 angular js 中使用变量

javascript - 如果选中复选框,则添加 £1.50 到总额中,如果不添加任何内容

c - Linux, C : terminate multple threads after some seconds (timer? )