简体   繁体   English

未定义的变量javascript - lightbox

[英]undefined variable javascript - lightbox

im trying to open a lightbox when webpages loads using the javascript library "lytebox" 我试图在使用javascript库“lytebox”加载网页时打开灯箱

here is my code 这是我的代码

<!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>

the code works fine on firefox and chrome, but on internet explorer 8 it doesn't works and I get the following error: 代码在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

How can I fix it? 我该如何解决?

ps im using lytebox because it doesnt need jquery, and i dont want cause conflicts with other parts on my page (like "videolightbox") ps我使用lytebox,因为它不需要jquery,我不希望与我的页面上的其他部分发生冲突(如“videolightbox”)

Here's a guess: Lytebox uses window.onload itself in order to set up the $lb variable (you can see this at the very end of the script ), and when you do <body onload"..."> you're basically clobbering Lytebox's window.onload function before it has a chance to fire. 这是一个猜测:Lytebox使用window.onload本身来设置$lb变量(你可以在脚本的最后看到这个),当你做<body onload"...">你基本上是在它有机会射击之前,破解Lytebox的window.onload功能。 Here's another SO question that seems to confirm this behavior in IE. 这是另一个SO问题似乎证实了IE中的这种行为。

So, how do you add an onload event when Lytebox needs its own? 那么,当Lytebox需要自己的时候,你如何添加onload事件呢? Simon Willison actually came up with a solution seven years ago in a rather classic article . 七年前,西蒙威利森在一篇颇为经典的文章中提出了一个解决方案。 In your case his advice is straightforward to implement: 在您的情况下,他的建议很容易实现:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM