javascript - Colorbox 无法在 IE 中正确加载

标签 javascript jquery internet-explorer jquery-plugins colorbox

我已经找到了解决方案,但它不是最好的,所以我仍在寻找解决方案。请参阅我的回答以了解我所做的事情。


更新 - 我的错误仍然存​​在,如下所述,但是如果我打开 IE 的开发人员工具,错误就会消失!如果我关闭浏览器并重新打开错误再次出现!

UPDATE 2 - 我尝试将以下代码插入我的 JS 以查看是否可以解决问题,但没有成功:

if (!("console" in window) || !("firebug" in console)) {
  var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
  window.console = {};
  for (var i = 0, len = names.length; i < len; ++i) {
    window.console[names[i]] = function(){};
  }
}

然后是:

var console = {};
console.log = function(){};

我还扫描了我的 JS 文件,但从未遇到会导致错误的 console.log 函数。


这是一个复杂的问题,我会尽力解释。我在一个页面中设置 cookie,该页面将在第一次访问时显示一个灯箱。它适用于 FF、Chrome 等,但不适用于 IE。

在 IE 中发生的是调用我的灯箱 (colorbox) 的脚本触发,但我看到的只是 AJAX 加载器旋转,内容从未加载。我发现脚本触发得太早了。我用的是 $j(document).ready(function()我切换到:$j(window).load(function()一切似乎都很好,并且工作正常,直到我从另一个页面开始并来到上面提到的页面。

如果我从任何其他页面开始并单击链接,我也会遇到同样的问题! cookie 工作正常,不会再次触发该框。

换句话说,如果我清除 cookie 并从有问题的页面开始,那么就没有问题。 但是 如果我从任何其他页面(已清除 cookie)开始并转到上面的页面,则颜色框无法正确加载。

据我所知 $j(window).load(function()无法正常工作。

我没有收到来自 IE 的错误。我正在使用 IE 8 进行测试,无法测试 9,因为我使用的是 Windows XP。 (有人告诉我它在 IE 9 中工作正常,但尚未证实这一点)脚本在 <head> 中我的文件。 (如果我将脚本移动到 <body> 中,它会完全破坏页面。)

我已阅读 DOCTYPE 的问题在 IE 中不正确或缩短和 colorbox 问题。我的 DOCTYPE 如下,应该是正确的:

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

非常感谢任何想法或想法!

这是我正在使用的代码:

Javascript

var $j = jQuery.noConflict();
$j(window).load(function() {
  //window.onload = function() does not function properly either...
  if(!$j.cookie('gallerycookie')){
    $j.colorbox({
      inline:true, 
      href:"#gallery-nav-instruct"
    });
    $j.cookie("gallerycookie", 1, {
      expires: 30, 
      path: '/'
    });
  }
});

HTML

<div style="display:none">
  <div id="gallery-nav-instruct">
    <h2>Gallery Navigation Instructions - Step 1</h2><h2 style="text-align:right">
      <a style="text-align:right;" class="inline cw" href="
         #gallery-enlarge-instruct">Step 2</a></h2>
    <p>&nbsp;</p>
    <p class="white"><img src="/Images/Pages/gallery-navigation.jpg" width="890" height="450" alt="Gallery Navigation Instructions" /></p>
  </div>
</div>

<div style="display:none">
  <div id="gallery-enlarge-instruct">
    <h2>Gallery Navigation Instructions - Step 2</h2>
    <p>&nbsp;</p>
    <h2><a class="inline cw" href="#gallery-nav-instruct">Step 1</a> </h2>
    <p class="white"><img src="/Images/Pages/gallery-enlarge.jpg" width="890" height="510" alt="Gallery -Enlarged View Instructions" /></p>
  </div>
</div>

另一个注意事项:我在页面上使用 jAlbum,我没有看到任何冲突,但会不会有问题?我不能在此处发布该代码,因为它会超过 SO 的发布限制。

要触发此错误,请从 this page 开始,将出现一个灯箱。点击Message Examples (灯箱的左下角或没有灯箱的菜单中的第一项)。

这是指向 page 的直接链接如果需要的话。 (直接进入页面不会触发错误。)

我尝试使用 window.onload = function()并且发生了同样的问题。

我尝试使用事件处理程序在加载 div 后触发脚本,但根本没有触发脚本。这是代码:

var $j = jQuery.noConflict();
$j('#gallery-nav-instruct').load(function() {
  if(!$j.cookie('gallerycookie')){
    $j.colorbox({
      inline:true, 
      href:"#gallery-nav-instruct"
    });
    $j.cookie("gallerycookie", 1, {
      expires: 30, 
      path: '/'
    });
  }
}); 

最佳答案

当您打开开发人员工具时它消失的原因是 IE 在您打开开发人员工具之前不能很好地处理任何 console.log 语句。某处必须有一个 console.log。它可以在您正在使用的任何插件文件或 javascript 文件中。由于某种原因,它会杀死在它之后运行的 javascript。打开开发人员工具将 console.log 识别为一个 javascript 函数(我最好的猜测),因此它突然起作用了。

关于javascript - Colorbox 无法在 IE 中正确加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8116191/

相关文章:

javascript - AngularJS:OrderBy Descending(空条目)

jQuery 函数 Window.height() 当设备方向改变时改变其高度

asp.net - 现有网站在 Internet Explorer 8 中正确呈现有哪些问题?

html - Div 在 Internet Explorer 上更长

javascript - IE 不支持 Array includes 或 String includes 方法

javascript - 为什么 jQuery 成功回调有时有 3 个参数,有时只有 1 个?

javascript - 如何滚动到文本区域中光标的位置?

javascript - 如何在 React-native 中显示来自 Firebase 存储的图像?

jquery - ASP.NET MVC 2 的文件管理器?

javascript - 删除循环内的多个 JSON 对象