IE7中的JavaScript错误& IE8:SCRIPT5007:无法获取属性'newQuestion'的值:object为null或undefined
我一直在寻找并无法提出解决方案。以下代码在IE9,Chrome,FF和Safari中完美运行。但是,当您在IE7或IE8中点击开始时会出现以下错误:
I've been looking all over and can't come up with a solution. The following code works perfectly in IE9, Chrome, FF, and Safari. However, when you hit "Start" in IE7 or IE8 you get the following error:
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'>√ Question "+(i+1)+"</div>";
}
else {
DOMelem(a).innerHTML+="<div id='answer' class='wrong'>× 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>";
}
}
您可以查看演示这里。
我真的认为我已经阅读了所有相关问题,但仍然无法想出解决方案。如果有任何其他信息有用,请告诉我。
I really think I've read about every related question, but still can't come up with a solution. Please let me know if any additional information would be helpful.
更新:
为了澄清,我正在测试在IE9中打开了兼容模式。如果有人确实安装了IE7或IE8,我会知道你的结果。无论如何,代码似乎要执行到这里:
To clarify, I am testing in IE9 with compatibility mode turned on. If anyone actually has IE7 or IE8 installed, I would live to know your results. Anyway, the code seems to execute up until here:
//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
。
Once the start button is clicked, I get an error because this.varName
returns as undefined
.
在您的代码中:
> //The quiz object
实际上是一个函数对象,而不是普通对象。您是否希望将其称为构造函数?我无法进入演示。
Is really a function object, not a plain object. And do you expect to call it as a constructor? I can't get to the demo.
可能这样:
> 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 属性所有,除非你在其他地方添加其他方式)。
Global variables are not enumerable in IE 8 (and probably earlier), so you never find the instance(s) of sQuiz you are looking for and this.varName remains undefined (actually, the object will not have a varName property at all, unless you add it some other way elsewhere).
你需要使用一些其他方法来引用实例 - 也许使用模块模式并保持一个闭包中的引用,或每个实例的引用数组。
You need to use some other method of referencing the instance(s)— perhaps using the module pattern and keeping a reference in a closure, or an array of references to each instance.