简体   繁体   English

JavaScript代码仅在IE中不起作用

[英]Javascript code doesn't work in IE only

I have just verified my javascript code works fine in all browsers except IE. 我刚刚验证了我的JavaScript代码在IE以外的所有浏览器中都能正常工作。 How is it possible that the script is correctly read and executed in Chrome, Safari... but IE finds some inexplicable error and refuses to run the code? 如何在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')
        });
});

Error message in IE9 IE9中的错误消息

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

Well this comes down to the history of JavaScript. 嗯,这可以归结为JavaScript的历史。

JavaScript was implemented based on ECMAScript: JavaScript是基于ECMAScript实现的:

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

Each and every single Web Browser provider (Mozilla, Google, Microsoft) decided that they didn't want to standardize JavaScript and they each came up with their own implementation of the ECMAScript, and thus their own JavaScript engine. 每个Web浏览器提供商(Mozilla,Google,Microsoft)都决定他们不想标准化JavaScript,他们各自提出了自己的ECMAScript实现,因而提出了自己的JavaScript引擎。

Thus, we programmers get a headache trying to write JavaScript that is compatible across all these different JavaScript engines because each of them read JavaScript in their own way (which answers your question of why IE finds some inexplicable error while the rest doesn't) 因此,我们的程序员在尝试编写兼容所有这些不同JavaScript引擎的JavaScript时会头疼,因为他们每个人都以自己的方式读取JavaScript(这回答了您有关IE为什么会发现一些莫名其妙的错误而其余的却找不到的错误的问题)

Fun Fact: Only Mozilla's implementation of ECMAScript is actually called "JavaScript". 有趣的事实:实际上只有Mozilla的ECMAScript实现被称为“ JavaScript”。

You should lookup on how to write JavaScript that is cross-compatible across different JavaScript engines. 您应该查找如何编写跨不同JavaScript引擎交叉兼容的JavaScript。

Use a javascript validation tool, such as JSLint to ensure maximum compability. 使用JavaScript验证工具(例如JSLint)来确保最大的兼容性。 This, since a single omitted character (such as ; , ' , etc) can cause your script to not be run in specific browsers. 这是因为单个省略的字符(例如;'等)可能导致您的脚本无法在特定的浏览器中运行。

JSLint will also provide different tips on what do to and not to do to provide even further compability. JSLint还将提供关于做什么和不做什么的不同技巧,以提供进一步的兼容性。

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

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