2011-09-24 73 views
0

我尝试打开使用JavaScript库"lytebox"未定义的javascript变量 - 收藏

这里的网页加载时,收藏是我的代码

<!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 Explorer 8它不起作用,我得到以下错误:

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 

我该如何解决它?

p.s.即时通讯使用lytebox,因为它并不需要jQuery和我不希望我的页面上的其他部分造成冲突(如“videolightbox”)

回答

2

这里有一个猜测:Lytebox使用window.onload本身为了建立$lb变量(可以在the script的末尾看到这一点),当你做<body onload"...">时,你基本上会在Lytebox的window.onload函数有可能触发之前破坏它。这里的another SO question似乎证实了这种行为在IE中。

那么,当Lytebox需要它自己的时候,如何添加onload事件? Simon Willison实际上在七年前在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>