javascript - IE 和 FF 中的编码问题。在 Safari 和 Chrome 中工作

标签 javascript jquery firefox google-chrome

这两天已经让我发疯了...我一直在尝试使用其外部 API 将大型卡特尔商店集成到网站中,但在使脚本正常工作时遇到了一些问题。它似乎在 Chrome 和 Safari 中按我想要的方式工作,但在 FF 和 IE 中却不行?我试图将产品放入的页面是 http://www.denimgeek.com/wp-content/featured.html (目前适用于 Chrome 和 Safari)。

页面的编码如下,我绝不是 Javascript 程序员,但知道为什么这在这些浏览器中不起作用吗? Firefox 一直说“控制台未定义”,它在 IE8 中不起作用,但在我进入开发人员工具并将页面模式更改为怪癖后可以吗?!这让我发疯!

页面的代码如下,我是否缺少一些简单的东西?

<html>
<head>


<link rel="stylesheet" type="text/css" href="http://www.denimgeek.com/wp-content/themes/atlantica/style.css">   

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">    
var subdomain = 'denimgeek';    
$(function() {
      function findImage(url, size) {
            if(!size) return url;
            var ext = url.match(/\.(\w{2,4}$)/)[1];
            var dir = url.substr(0, url.lastIndexOf('/') + 1);
            switch(size.toLowerCase()) {
                case 'large':
                    return dir + '300.' + ext;
                case 'medium':
                    return dir + '175.' + ext;
                case 'thumb':
                    return dir + '75.' + ext;
                default:
                    return url;
            }
      }
      $.getJSON('http://api.bigcartel.com/' + subdomain + '/products.js?callback=?', function(products) {
      $.each(products, function(i, product) {
          console.log(product);
          $('#products').append('<li><img src="' + findImage(product.images[0].url, 'large') + '"><br><a href="http://' + subdomain + '.bigcartel.com' + product.url + '">' + product.name + '</a></li>');
        });
      });
    });
  </script>
</head>  <body>  <ol id="products"></ol></body></html> 

最佳答案

好吧..在您的Javascript代码中,我们可以找到:

console.log(product);  

当没有定义控制台对象时,它会抛出错误。 控制台对象例如由FirebugChrome开发者工具以及IE8+开发者工具定义。 p>

但是如果您禁用这些工具,无论哪种方式都会抛出异常。一个好的做法是你自己去捕捉它。我在我的初始化脚本中使用它:

if(!('console' in window) ) {
    window.console = {}

    var arr = 'log error warn time timeEnd table profile profileEnd group groupEnd debug dir'.split(' ');

    for(var i = 0, len = arr.length; i < len; i++) {
        console[arr[i]] = function() { };
    }
}

如果您将该代码片段放在脚本开头的某个位置,这些 console.anything 调试输出将不会导致整个应用程序崩溃。

关于javascript - IE 和 FF 中的编码问题。在 Safari 和 Chrome 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5272921/

相关文章:

javascript - 在协程中包装 ES6 类方法

javascript - 使用removeChild()从父DOM中删除特定索引子级[Vanilla Javascript]

javascript - Jquery 选择子项中的所有子项

css - 为什么 jquery ui 按钮在 Firefox 和 Chrome 中看起来不同

javascript - 当我更改其在 DOM 中的位置时,如何停止重新加载 iframe?

javascript - div - createElement 不起作用?

javascript - jQuery datepicker解析日期抛出无效日期异常

jquery - Jquery 上的火灾事件检查/取消检查

php - Laravel 动态数据填充在 Bootstrap 模式中

firefox - 使用 Firefox 3.5 在 HTML 中内联 SVG