两个javascripts相互冲突

时间:2012-12-20 20:03:04

标签: javascript

我搜索过每一个可能的网站,我已经联系了我们的网站开发人员,他不是真正的专业设计师或编码员,而且我被困住了。我在我的主页上运行了两个脚本,根据她的说法,只要有两个,“NewsRotator”脚本就会被取消并且功能不正确。从我读过的所有内容来看,它听起来可能是window.onload命令的问题,但我仍然对这个javascript的东西很新。

我将在这里发布编程,如果有人可以帮助,我们将不胜感激。首先,新闻旋转器,一个出现故障:

<div id="NewsBox">
<div id="NewsContainer">
  <div id="NewsRotator">
    <div>
  <img src="images/loading.gif" border="0" alt="Loading..."><br>Loading...
}    
</div>
  </div>
<div style="clear:both;height:1px;overflow:hidden"></div>
</div>
<div id="NewsPreview"></div>
<div id="NewsItems" style="display:none">
[!Ditto? &startID=`[[CurrentPress]]` &summarize=`4` &tpl=`NewsRotatorArticle`      &sortBy=`pub_date` &sortDir=`DESC`!]
</div>
</div>
<script language="javascript">
// Change the value below to modify the delay between news items.
var delaySeconds = 8;
var currentItem = 0;
var timer = null;
var newsItems;
var articles = new Array();

window.onload=initializeItems;

// Intialize Variables
function initializeItems() {
init();

newsItems = document.getElementById('NewsItems');
var divs = newsItems.getElementsByTagName('DIV');
for (var i=0;i<divs.length;i++) {
    if (divs[i].getAttribute("type") == 'article')
        articles[articles.length] = divs[i];
}
displayItem();
}

// Display item
function displayItem() {
if (currentItem > articles.length - 1) currentItem = 0;
if (articles[currentItem]) {
var url = articles[currentItem].getAttribute("url");
var html = articles[currentItem].innerHTML;


document.getElementById("NewsRotator").innerHTML = html;
var newslink = document.getElementById("NewsLink");
var links = newslink.getElementsByTagName("A");
links[0].href = url;
newslink.style.display = "block";

currentItem++;
updatePreview();
timer = window.setTimeout("displayItem()",delaySeconds*1000);
}
}

function updatePreview() {
    var imgs = new Array();
    var html = "";

    // Get main image from each article;
for (var i=0; i<articles.length; i++) {
    var tmp = articles[i].getElementsByTagName("IMG");
    if (tmp && tmp.length > 0)
        imgs[imgs.length] = tmp[0].src;
    else
        imgs[imgs.length] = null;
}

var end = (currentItem > articles.length-1) ? articles.length-1 : currentItem;
var begin = end-3;
if (begin < 0) {
    begin = 0;
    end = (articles.length >= 4) ? 3 : articles.length-1;
}

for (var j=begin; j<=end; j++) {
    //alert('begin: ' + begin + "\n end:" + end);
    if (imgs[j] != null) {
       if(j+1 == currentItem) {
           html += '<li><a href="#" onclick="currentItem=' + j + ';window.clearTimeout(timer);displayItem();return false;" class="current">';
           html += '<img src="/images/news/prev'+(j+1)+'-over.gif" alt="'+(j+1)+'" border="0"></a></li>' + "\n";+'</a></li>' + "\n";
       }
       else {
           html += '<li><a href="#" onclick="currentItem=' + j + ';window.clearTimeout(timer);displayItem();return false;">';
           html += '<img src="/images/news/prev'+(j+1)+'.gif" alt="'+(j+1)+'" border="0"></a></li>' + "\n";+'</a></li>' + "\n";
       }
    }
}

if (html != "") {
    html = '<ul>' + html + '</ul><div style="clear:both"></div>';
    document.getElementById('NewsPreview').innerHTML = html;
}
}
</script>

这是第二个,当它在页面上时效果很好,但它会关闭我的新闻旋转器。

//Specify the slider's width (in pixels)
var sliderwidth="300px"
//Specify the slider's height
var sliderheight="150px"
//Specify the slider's slide speed (larger is faster 1-10)
var slidespeed=3
//configure background color:
slidebgcolor="#EAEAEA"

//Specify the slider's images
var leftrightslide=new Array()
var finalslide=''
//Specify the slider's width (in pixels)
var sliderwidth="940px"
//Specify the slider's height
var sliderheight="125px"
//Specify the slider's slide speed (larger is faster 1-10)
var slidespeed=2
//configure background color:
slidebgcolor="#ffffff"

//Specify the slider's images
var leftrightslide=new Array()
var finalslide=''
leftrightslide[0]='<a href="http://deals.sheraton.com/promos/NAD-massachusetts-Springfield-falcons-hockey"><img src="assets/images/new web graphics/Corporate Logos/sheraton.png" border=1></a>'
leftrightslide[1]='<a href="http://www.libboslaw.com"><img src="assets/images/new web graphics/Corporate Logos//libboslaw.png" border=1></a>'
leftrightslide[2]='<a href="http://www.amicaconnections.com"><img src="assets/images/new web graphics/Corporate Logos/amica.png" border=1></a>'
leftrightslide[3]='<a href="http://www.artiolidodge.com"><img src="assets/images/new web graphics/Corporate Logos/artiolidodge.png" border=1></a>'
leftrightslide[4]='<a href="http://www.bigy.com"><img src="assets/images/new web graphics/Corporate Logos/bigy.png" border=1></a>'
leftrightslide[5]='<a href="http://www.bltees.com"><img src="assets/images/new web graphics/Corporate Logos/bltees.png" border=1></a>'
leftrightslide[6]='<a href="http://www.completepayrollsolutions.com"><img src="assets/images/new web graphics/Corporate Logos/cps.png" border=1></a>'
leftrightslide[7]='<a href="http://www.baliseauto.com"><img src="assets/images/new web graphics/Corporate Logos/balise.png" border=1></a>'
leftrightslide[8]='<a href="http://www.facebook.com/70054351104/posts/10151037727871105"><img src="assets/images/new web graphics/Corporate Logos/garyrome.png" border=1></a>'
leftrightslide[9]='<a href="http://www.mylimo5.com"><img src="assets/images/new web graphics/Corporate Logos/exclusivelimo.png" border=1></a>'
leftrightslide[10]='<a href="http://www.businesswest.com"><img src="assets/images/new web graphics/Corporate Logos/businesswest.png" border=1></a>'
leftrightslide[11]='<a href="http://www.springfieldmagnet.com/website"><img src="assets/images/new web graphics/Corporate Logos/magnetschools.png" border=1></a>'
leftrightslide[12]='<a href="http://www.mymamamiaspizza.com"><img src="assets/images/new web graphics/Corporate Logos/mama mias.png" border=1></a>'
leftrightslide[13]='<a href="http://www.maplegaterehab.com"><img src="assets/images/new web graphics/Corporate Logos/maplegate.png" border=1></a>'
leftrightslide[14]='<a href="http://www.massmutual.com"><img src="assets/images/new web graphics/Corporate Logos/massmutual.png" border=1></a>'
leftrightslide[15]='<a href="http://www.mricenter.com/locations/Massachusetts_Springfield.htm"><img src="assets/images/new web graphics/Corporate Logos/mricenter.png" border=1></a>'
leftrightslide[16]='<a href="http://www.hampdenbank.com"><img src="assets/images/new web graphics/Corporate Logos/hampdenbank.png" border=1></a>'
leftrightslide[17]='<a href="http://www.springfielddowntown.com"><img src="assets/images/new web graphics/Corporate Logos/sbid.png" border=1></a>'
leftrightslide[18]='<a href="http://www.jtssportspub.com"><img src="assets/images/new web graphics/Corporate Logos/jts.png" border=1></a>'
leftrightslide[19]='<a href="http://www.wne.edu"><img src="assets/images/new web graphics/Corporate Logos/wne.png" border=1></a>'
leftrightslide[20]='<a href="http://www.reminderpublications.com"><img src="assets/images/new web graphics/Corporate Logos/reminder.png" border=1></a>'
leftrightslide[21]='<a href="http://www.theobbq.com"><img src="assets/images/new web graphics/Corporate Logos/theodores.png" border=1></a>'
leftrightslide[22]='<a href="http://forms.prideaccounts.com/gas.html"><img src="assets/images/new web graphics/Corporate Logos/pride.png" border=1></a>'
leftrightslide[23]='<a href="http://www.reliabletemps.com"><img src="assets/images/new web graphics/Corporate Logos/reliabletemps.png" border=1></a>'

//Specify gap between each image (use HTML):
var imagegap="<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>"

//Specify pixels gap between each slideshow rotation (use integer):
var slideshowgap=25


//Specify gap between each image (use HTML):
var imagegap=" "

//Specify pixels gap between each slideshow rotation (use integer):
var slideshowgap=5


////NO NEED TO EDIT BELOW THIS LINE////////////

var copyspeed=slidespeed
leftrightslide='<nobr>'+leftrightslide.join(imagegap)+'</nobr>'
var iedom=document.all||document.getElementById
if (iedom)
document.write('<span id="temp" style="visibility:hidden;position:absolute;top:-100px;left:-9000px">'+leftrightslide+'</span>')
var actualwidth=''
var cross_slide, ns_slide

function fillup(){
if (iedom){
cross_slide=document.getElementById? document.getElementById("test2") : document.all.test2
cross_slide2=document.getElementById? document.getElementById("test3") : document.all.test3
cross_slide.innerHTML=cross_slide2.innerHTML=leftrightslide
actualwidth=document.all? cross_slide.offsetWidth : document.getElementById("temp").offsetWidth
cross_slide2.style.left=actualwidth+slideshowgap+"px"
}
else if (document.layers){
ns_slide=document.ns_slidemenu.document.ns_slidemenu2
ns_slide2=document.ns_slidemenu.document.ns_slidemenu3
ns_slide.document.write(leftrightslide)
ns_slide.document.close()
actualwidth=ns_slide.document.width
ns_slide2.left=actualwidth+slideshowgap
ns_slide2.document.write(leftrightslide)
ns_slide2.document.close()
}
lefttime=setInterval("slideleft()",30)
}
window.onload=delay

function slideleft(){
if (iedom){
if (parseInt(cross_slide.style.left)>(actualwidth*(-1)+8))
cross_slide.style.left=parseInt(cross_slide.style.left)-copyspeed+"px"
else
cross_slide.style.left=parseInt(cross_slide2.style.left)+actualwidth+slideshowgap+"px"

if (parseInt(cross_slide2.style.left)>(actualwidth*(-1)+8))
cross_slide2.style.left=parseInt(cross_slide2.style.left)-copyspeed+"px"
else
cross_slide2.style.left=parseInt(cross_slide.style.left)+actualwidth+slideshowgap+"px"

}
else if (document.layers){
if (ns_slide.left>(actualwidth*(-1)+8))
ns_slide.left-=copyspeed
else
ns_slide.left=ns_slide2.left+actualwidth+slideshowgap

if (ns_slide2.left>(actualwidth*(-1)+8))
ns_slide2.left-=copyspeed
else
ns_slide2.left=ns_slide.left+actualwidth+slideshowgap
}
}


if (iedom||document.layers){
with (document){
document.write('<table border="0" cellspacing="0" cellpadding="0"><td>')
if (iedom){
write('<div style="position:relative;width:'+sliderwidth+';height:'+sliderheight+';overflow:hidden">')
write('<div style="position:absolute;width:'+sliderwidth+';height:'+sliderheight+';background-color:'+slidebgcolor+'" onMouseover="copyspeed=0" onMouseout="copyspeed=slidespeed">')
write('<div id="test2" style="position:absolute;left:0px;top:0px"></div>')
write('<div id="test3" style="position:absolute;left:-1000px;top:0px"></div>')
write('</div></div>')
}
else if (document.layers){
write('<ilayer width='+sliderwidth+' height='+sliderheight+' name="ns_slidemenu" bgColor='+slidebgcolor+'>')
write('<layer name="ns_slidemenu2" left=0 top=0 onMouseover="copyspeed=0" onMouseout="copyspeed=slidespeed"></layer>')
write('<layer name="ns_slidemenu3" left=0 top=0 onMouseover="copyspeed=0" onMouseout="copyspeed=slidespeed"></layer>')
write('</ilayer>')
}
document.write('</td></table>')
}
}

1 个答案:

答案 0 :(得分:1)

这不是你的答案,所以我不希望它被接受,但是这段代码肯定没有帮助,并指出你的冲突脚本可能正在做什么。每当在全局范围(或​​任何共享范围)中重新声明变量时,它将覆盖先前的声明。在这里,您将覆盖初始声明下面几行的两个变量,可能是复制/粘贴错误:

//Specify gap between each image (use HTML):
var imagegap="<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>"

//Specify pixels gap between each slideshow rotation (use integer):
var slideshowgap=25


//Specify gap between each image (use HTML):
var imagegap=" "

//Specify pixels gap between each slideshow rotation (use integer):
var slideshowgap=5

Javascript使用函数来控制范围,因此限制冲突的一种方法是将脚本包装在一个自封闭的匿名函数中,如下所示:

(function() {
    // your 1st script code here
    var foo = 'foo';
}());

(function() {
    // your 2nd script code here
    var foo = 'bar'; // will not overwrite the 1st script's foo because of the controlled scope
}());

您要做的另一件事是保持全局范围清洁。全局范围仅指分配给全局对象的属性 - 对于将为window的DOM。所以这个:

<script type="text/javascript">
    var foo = 'foo';
</script>

...与此相同:

<script type="text/javascript">
    window.foo = 'foo';
</script>

...并且由于在任何给定时间点只能有window.foo的一个值,因此很容易被意外覆盖。