Javascript代码不仅在IE中工作

Javascript code doesn't work in IE only

本文关键字:IE 工作 代码 Javascript      更新时间:2023-09-26

我刚刚验证了我的javascript代码在除IE以外的所有浏览器中都能正常工作。脚本怎么可能在Chrome,Safari中正确读取和执行...但是IE发现了一些莫名其妙的错误并拒绝运行代码?

$(document).ready(function() {
        var NewsNavigator = {
            init: function(config) {
                this.news = config.news;
                this.newsNum = this.news.length;
                this.navbuttons = config.navbuttons;
                this.prevButton = config.prevButton;
                this.nextButton = config.nextButton;
                this.displayatonce = config.displayatonce;
                this.maxSteps = Math.ceil(this.newsNum / this.displayatonce);
                this.counter = 0;
                this.navigateNews();
                this.enableNav();
                this.checkButtonsVisibility();
            },
            showNews: function() {
                var start = this.counter * this.displayatonce;
                var end = this.counter * this.displayatonce + (this.displayatonce - 1);
                for (i=start; i<=end; i++) {
                    this.news.eq(i).show();
                }
            },
            hideAllNews: function() {
                console.log("hiding news");
                this.news.hide();
            },
            navigateNews: function() {
                this.hideAllNews();
                this.showNews();
            },
            checkButtonsVisibility: function() {
                if (this.counter <= 0)
                {
                    this.prevButton.css('visibility', 'hidden');
                }
                else
                {
                    this.prevButton.css('visibility', 'visible');
                }
                if (this.counter >= this.maxSteps - 1) 
                {
                    this.nextButton.css('visibility', 'hidden');
                }
                else
                {
                    this.nextButton.css('visibility', 'visible');
                }
            },
            enableNav: function() {
                self = this;
                this.navbuttons.on('click', function(event) {
                    if (($(this).data('dir') === 'prev') && (self.counter > 0)) {
                        self.counter--;
                        self.navigateNews();
                    } else if (($(this).data('dir') === 'next') && (self.counter < self.maxSteps - 1)) {
                        self.counter++;
                        self.navigateNews();
                    }
                self.checkButtonsVisibility();
                event.preventDefault(); 
                });
            }
        };
        NewsNavigator.init({
            news: $('div#cat-news').find('div.oneCol'),
            displayatonce: 3,
            navbuttons: $('div#nav').find('a'),
            prevButton: $('div#nav a.prec'),
            nextButton: $('div#nav a.succ')
        });
});

IE9 中的错误消息

SCRIPT438: The object doesn't support the 'checkButtonsVisibility' property or method.
NewsNavigator.js, Row 69 Character 5

这归结为JavaScript的历史。

JavaScript 是基于 ECMAScript 实现的:

http://en.wikipedia.org/wiki/ECMAScript

每个 Web 浏览器提供商(Mozilla、Google Microsoft)都决定他们不想标准化 JavaScript,他们每个人都提出了自己的 ECMAScript 实现,从而提出了自己的 JavaScript 引擎。

因此,我们程序员在尝试编写与所有这些不同的JavaScript引擎兼容的JavaScript时会感到头疼,因为他们每个人都以自己的方式阅读JavaScript(这回答了为什么IE会发现一些莫名其妙的错误而其余的没有)

有趣的事实:只有 Mozilla 的 ECMAScript 实现实际上被称为"JavaScript"。

你应该查找如何编写跨不同 JavaScript 引擎交叉兼容的 JavaScript。

使用 JavaScript 验证工具(如 JSLint)来确保最大的兼容性。这是因为单个省略的字符(如 ;' 等)可能会导致脚本无法在特定浏览器中运行。

JSLint 还将提供关于做什么和不做什么的不同提示,以提供进一步的兼容性。