2011-09-24 65 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>