当使用javascript库"lytebox"加载网页时,我正在尝试打开灯箱
以下是我的代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" language="javascript" src="lytebox.js"></script>
<link rel="stylesheet" href="lytebox.css" type="text/css" media="screen" />
<script>
function ShowIntro()
{
$lb.launch('example.jpg','showPrint:true','Orion Nebula','This is my description, which is optional');
}
</script>
</head>
<body onLoad="ShowIntro()">
</body>
</html>代码在firefox和chrome上运行良好,但在internet Explorer8上不能运行,我得到了以下错误:
Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)
Timestamp: Sat, 24 Sep 2011 05:10:04 UTC
Message: '$lb' is undefined
Line: 12
Char: 3
Code: 0
URI: file:///C:/Users/User/Desktop/lytebox_v5.1/lytebox.html我怎么才能修复它?
附注:我使用lytebox是因为它不需要jquery,而且我不想与页面上的其他部分发生冲突(比如"videolightbox")
发布于 2011-09-24 13:58:24
这里有一个猜测: Lytebox使用window.onload本身来设置$lb变量(您可以在the script的末尾看到这一点),当您执行<body onload"...">时,您基本上是在Lytebox的window.onload函数有机会触发之前破坏它。下面的another SO question似乎证实了IE中的这种行为。
那么,当Lytebox需要自己的事件时,如何添加onload事件呢?事实上,西蒙·威里森七年前就在a rather classic article提出了一个解决方案。在您的案例中,他的建议很容易实现:
<body><!-- no onload --->
<script>
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = ShowIntro;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
ShowIntro();
}
}
</script>
</body>https://stackoverflow.com/questions/7537200
复制相似问题