Javascript 脚本错误,如果发生,则添加到何处
我有这个脚本,但在IE8中我得到了一个错误:“照片是未定义的”,如果要解决这个问题,应该怎么做:Javascript 脚本错误,如果发生,则添加到何处,javascript,jquery,html,internet-explorer-8,fatal-error,Javascript,Jquery,Html,Internet Explorer 8,Fatal Error,我有这个脚本,但在IE8中我得到了一个错误:“照片是未定义的”,如果要解决这个问题,应该怎么做: /* * Author: Marco Kuiper (http://www.marcofolio.net/) */ window.addEvent('load', function() { jQuery("#slideimg1").css({ "background-image" : "url("+url+"/" + photos[0].image
/*
* 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);
}
});
我注意到您使用了“photos”,但我没有看到在任何地方声明变量。。。
至少,在主函数中(因为它不是全局函数),声明您的照片变量:
var photos = [];
另外,如Chris所说,始终检查var是否已定义。:)
资料来源:
在其他浏览器中可以吗???您没有在代码中定义
照片
。询问代码的问题必须表明对正在解决的问题的最低理解。包括尝试过的解决方案、它们不起作用的原因以及预期结果。在firefox和chrome中都可以,在ie9和ie10中也可以,在ie8中我在主页上也出现了错误“第0行”,我无法调试这个错误,因为我在这里问…@Jeff Noel-在我把这个问题发布到这里之前,我在更多浏览器上测试了这个问题,包括控制台、chrome、firefox、ie和他的moduses。。。这个问题出现在ie8和modus“stanard”上,如果照片未声明,那么第一个和第三个如果将失败。您必须使用if(window.photos)
或第二个if
语句,否则您将得到一个未捕获的引用错误。-因为照片没有申报因为我们为未声明的变量添加了一个适当的检查。我使用这个,这很有效(if(typeof(photos)!=“undefined”),谢谢you@BrandonBoone当然,这就是我另外说的原因,因为我用var photos=[]声明了它代码>:)
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