2013-07-19 25 views
-5

我有这个剧本,但在IE8我得到错误添加:“照片是undiefienied”,哪里是如果做来解决这个问题:脚本错误,在这里如果

/* 
* Author:  Marco Kuiper (http://www.marcofolio.net/) 
*/ 
window.addEvent('load', function() { 

    jQuery("#slideimg1").css({ 
      "background-image" : "url("+url+"/" + photos[0].image + ")" 
    }); 
    if(photos.length >= 2){ 
     jQuery("#slideimg2").css({ 
       "background-image" : "url("+url+"/" + photos[1].image + ")" 
     }); 
    } 


    // Backwards navigation 
    jQuery("#cp-back").click(function() { 
     //stopAnimation(); 
     navigate("back"); 
    }); 
    // Forward navigation 
    jQuery("#cp-next").click(function() { 
     //stopAnimation(); 
     navigate("next"); 
    }); 


    //jQuery("#preload").hide(); 
    var activeContainer = 1;  
    var currentImg = 0; 
    var animating = false; 
    var first = false; 
    var navigate = function(direction) { 
     // Check if no animation is running. If it is, prevent the action 
     if(animating) { 
      return; 
     } 
     // Check which current image we need to show 
     if(direction == "next") { 
      currentImg++; 
      if(currentImg == photos.length + 1) { 
       currentImg = 1; 
      } 
     } else { 
      currentImg--; 
      if(currentImg == 0) { 
       currentImg = photos.length; 
      } 
     } 

     // Check which container we need to use 
     var currentContainer = activeContainer; 
     if(activeContainer == 1) { 
      activeContainer = 2; 
     } else { 
      activeContainer = 1; 
     } 

     showImage(photos[currentImg - 1], currentContainer, activeContainer); 

    }; 

    var currentZindex = -1; 
    var showImage = function(photoObject, currentContainer, activeContainer) { 
     //alert(currentContainer); 
     animating = true; 
     // Make sure the new container is always on the background 
     currentZindex--; 
     if(!first){ 
      //alert(first); 
      //alert("aa"); 
      // Set the background image of the new active container 
      jQuery("#slideimg" + activeContainer).css({ 
       "background-image" : "url("+url+"/" + photoObject.image + ")" 
       //"display" : "block", 
       //"z-index" : currentZindex 
      }); 
      // Fade out the current container 
      // and display the header text when animation is complete 
      jQuery("#slideimg" + currentContainer).fadeOut(effectTime,function() { 
       animating = false; 
      }); 
      jQuery("#slideimg" + activeContainer).fadeIn(effectTime); 

      //first = false; 
     }else{ 
      //alert("bbb"); 
      jQuery("#slideimg" + activeContainer).fadeOut(effectTime,function() { 
       animating = false; 
      }); 
      jQuery("#slideimg" + currentContainer).fadeIn(effectTime); 
      first = false; 
      //animating = false; 
     } 
    }; 

    var stopAnimation = function() { 
     // Clear the interval 
     clearInterval(interval); 
    }; 

    // We should statically set the first image 
    navigate("next"); 

    if(photos.length > 1){ 
     // Start playing the animation 
     interval = setInterval(function() { 
      navigate("next"); 
     }, slideshowSpeed); 
    } 

}); 
+0

??? – Cherniv

+1

你还没有在你的代码中定义'照片' –

+5

*询问代码的问题必须**显示对所解决问题的最小理解** **包括尝试的解决方案,为什么他们不工作**,以及预期的结果。* [如何使用浏览器的控制台](https://developers.google.com/chrome-developer-tools/docs/console?hl=fr) –

回答

1

我注意到你的“照片”的用法,但我没有看到声明的变量的任何地方...... 至少,主要功能(因为它不会是全球性的)里面,声明你的照片变种:

var photos = []; 

像克里斯说的那样,常常检查var是否被定义。 :) 来源:https://stackoverflow.com/a/17748905/2599797

if (photos) // Simple 
if (typeof(photos) != 'undefined') // Type matching, if photos defaults to true it won't pass 
if ($.isArray(photos)) // jQuery based Javascript/Prototype native array check 
在其他浏览器其确定
+0

如果照片未申报,那么你的第一个和第三个“if”将失败。你必须使用if(window.photos)或你的第二个if语句,否则你会得到一个未捕获的引用错误。 [Broken](http://jsfiddle.net/tEg8j/2/) - 由于照片未申报。 [Fixed](http://jsfiddle.net/tEg8j/3/) - 因为我们为未声明的变量添加了适当的检查。 –

+0

我使用这个,这是行之有效的(如果(typeof(photos)!='undefined')),比如你自己 –

+0

@BrandonBoone,这就是为什么我说*额外*,因为我用'var photos = []声明了它。 '。 :) – ReSpawN