IE7中的JavaScript错误& IE8:SCRIPT5007:无法获取属性'newQuestion'的值:object为null或undefined

时间:2012-01-17 04:55:39

标签: javascript internet-explorer-8 internet-explorer-7

我一直在寻找并且无法提出解决方案。以下代码在IE9,Chrome,FF和Safari中完美运行。但是,当您在IE7或IE8中点击“开始”时,会出现以下错误:

SCRIPT5007: Unable to get the value of property 'newQuestion': object is null or undefined

有争议的代码在这里:

//This is used to reduce some code size and not type document.getElementById(a) each time
function DOMelem(a) {
    return document.getElementById(a);
}
//This is used to round numbers
function roundTo(a,b) {
    a=a*Math.pow(10,b);
    a=Math.round(a);
    a=a/Math.pow(10,b);
    return a;
}
//The quiz object
function sQuiz(a,c,init) {
    //This part is used to get the variable used by the user
    this.getName=function() {
        for(var v in window) {
            if(window[v] instanceof sQuiz) {
                this.varName=v;
            }
        }   
    },
    //This will create the first page of the quiz
    this.createQuiz=function() {
        this.getName();
        DOMelem(a).innerHTML="<h1>"+init.intro.qTitle+"</h1>";
        DOMelem(a).innerHTML+="<h2>"+init.intro.qAuthor+"</h2>";
        DOMelem(a).innerHTML+="<h3>"+init.intro.qComments+"</h3><br /><br />";
        DOMelem(a).innerHTML+="<input type='button' value='Start' onClick='"+this.varName+".newQuestion(0)'>";
    },
    //This one will create a random id for the quiz and create a bar for it
    this.createBar=function() {
        id="12391023103091"+Math.round(Math.random()*12391023103091);
        id=id.substr(id.length-14,14);
        this.randBarId=id;
        DOMelem(c).innerHTML="<div class='progress_full' id='pro_"+id+"'><div class='progress_bar' style='width:0%;' id='bar_"+id+"'></div></div>";
    },
    //In this section we check to see if there's a bar, if we must show the results or just show the interface.
    this.newQuestion=function(index) {
        //Create the bar if it does not exist
        if(!DOMelem("pro_"+this.randBarId)||!DOMelem("bar_"+this.randBarId)) {
            eval(this.varName+".createBar()");
        }
        //Show the results page
        if(index>=init.questions.length) {
            eval(this.varName+".showResults()");
        }
        //Show the question interface
        else {
            DOMelem(a).innerHTML="<h1>"+init.questions[index].question+"</h1>";
            for (var i=0;i<init.questions[index].answers.length;i++) {
                DOMelem(a).innerHTML+="<input type='button' name='"+index+"_"+i+"' value='"+init.questions[index].answers[i]+"' onClick='"+this.varName+".checkAnswer("+index+","+i+")'>";
            }
            DOMelem(a).innerHTML+="<input type='submit' id='btn_"+this.randBarId+"' value='Next Question' onClick='"+this.varName+".changeQuestion("+index+")' disabled>";
            DOMelem("bar_"+this.randBarId).style.width=(((index+1)/init.questions.length)*100)+"%";
        }
    },
    //This is used when the user clicks to change the question 
    this.changeQuestion=function(index) {
        return this.newQuestion(index+1);
    },
    //This part handles when the user clicks on an answer, color it, un-disable the button and record the guess
    this.checkAnswer=function(index,i) {
        init.questions[index].guessed=i;
        var inputs=document.getElementById(a).getElementsByTagName("input");
        for(var j=0;j<inputs.length;j++) {
            document.getElementById(a).getElementsByTagName("input")[j].className="";
            if(inputs[i].name==index+"_"+i) {
                document.getElementById(a).getElementsByTagName("input")[i].className="selected";
            }
        }
        DOMelem("btn_"+this.randBarId).disabled=false;
    },
    //This one here shows the result of the quiz.
    this.showResults=function() {
        DOMelem(a).innerHTML="<h1>Results of "+init.intro.qTitle+"</h1>";
        var count=0;
        for(var i=0;i<init.questions.length;i++) {
            if(init.questions[i].guessed==(init.questions[i].correctAnswer-1)) {
                count++;
                DOMelem(a).innerHTML+="<div id='answer' class='correct'>&radic; Question "+(i+1)+"</div>";
            }
            else {
                DOMelem(a).innerHTML+="<div id='answer' class='wrong'>&times; Question "+(i+1)+"</div>";
            }
        }
        var result=roundTo((count/init.questions.length)*100,2);
        DOMelem(a).innerHTML+="<h3 style='clear:both'><br />"+result+"% correct answers.</h3>";
        var tweet=init.tweetText.replace(/--score--/gi,result+"%");
        tweet=tweet.replace(/ /gi,'+');
        tweet="https://www.twitter.com/share?text="+tweet;
        DOMelem(a).innerHTML+="<a href='"+tweet+"'>Tweet it</a>";
    }   
}

您可以查看演示here

我真的认为我已经阅读了所有相关问题,但仍无法提出解决方案。如果有任何其他信息有用,请告诉我。

更新:

为了澄清,我在IE9中测试兼容模式已打开。如果有人确实安装了IE7或IE8,我会知道你的结果。无论如何,代码似乎要执行到这里:

    //This will create the first page of the quiz
    this.createQuiz=function() {
        this.getName();
        DOMelem(a).innerHTML="<h1>"+init.intro.qTitle+"</h1>";
        DOMelem(a).innerHTML+="<h2>"+init.intro.qAuthor+"</h2>";
        DOMelem(a).innerHTML+="<h3>"+init.intro.qComments+"</h3><br /><br />";
        DOMelem(a).innerHTML+="<input type='button' value='Start' onClick='"+this.varName+".newQuestion(0)'>";
    },

点击开始按钮后,我收到错误消息,因为this.varName返回undefined

2 个答案:

答案 0 :(得分:2)

在您的代码中:

> //The quiz object

实际上是一个函数对象,而不是普通对象。你期望把它称为构造函数吗?我无法进入演示。

可能这样:

> this.getName=function() {
>     for(var v in window) {
>         if(window[v] instanceof sQuiz) {
>             this.varName=v;
>         }
>     }
> },

被视为语法错误,因为它以逗号而不是分号结束。不,不是。

修改

你的问题在这里:

    for(var v in window) {
         if(window[v] instanceof sQuiz) {
             this.varName=v;
         }
    }

全局变量在IE 8中(并且可能更早)不可枚举,因此您永远找不到您要查找的sQuiz实例,并且 this.varName 仍未定义(实际上,对象)将完全没有 varName 属性,除非您在其他地方以其他方式添加它。)

您需要使用其他一些引用实例的方法 - 可能使用模块模式并在闭包中保留引用,或者对每个实例引用一组引用。

答案 1 :(得分:0)

我不是百分百确定,但是当我在IE中加载页面时出现JavaScript错误。这一行: -

if(window[v] instanceof sQuiz)

失败 - 您应该在instanceof

之前先检查窗口[v]是否为null