IE7 和 IE8 中的 JavaScript 错误:SCRIPT5007:无法获取属性“newQuestion”的值:对象为空或未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8889841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript error in IE7 & IE8: SCRIPT5007: Unable to get the value of property 'newQuestion': object is null or undefined
提问by ARP
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:
我一直在寻找,但无法提出解决方案。以下代码在 IE9、Chrome、FF 和 Safari 中完美运行。但是,当您在 IE7 或 IE8 中点击“开始”时,您会收到以下错误:
SCRIPT5007: Unable to get the value of property 'newQuestion': object is null or undefined
The code at issue is here:
有问题的代码在这里:
//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>";
}
}
You can view a demo here.
您可以在此处查看演示。
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.
我真的认为我已经阅读了所有相关问题,但仍然无法提出解决方案。如果有任何其他信息会有所帮助,请告诉我。
UPDATE:
更新:
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:
澄清一下,我正在 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)'>";
},
Once the start button is clicked, I get an error because this.varName
returns as undefined
.
单击开始按钮后,我收到一个错误,因为this.varName
返回为undefined
.
回答by RobG
In your code:
在您的代码中:
> //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.
真的是一个函数对象,而不是一个普通的对象。您是否希望将其称为构造函数?我无法进入演示。
Likely this:
大概是这样:
> this.getName=function() {
> for(var v in window) {
> if(window[v] instanceof sQuiz) {
> this.varName=v;
> }
> }
> },
is treated as a syntax error because it is terminated with a comma instead of a semi–colon. No, it's not.
被视为语法错误,因为它以逗号而不是分号结尾。不,这不对。
Edit
编辑
Your problem is here:
你的问题在这里:
for(var v in window) {
if(window[v] instanceof sQuiz) {
this.varName=v;
}
}
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.varNameremains undefined (actually, the object will not have a varNameproperty at all, unless you add it some other way elsewhere).
全局变量在 IE 8(可能更早版本)中是不可枚举的,所以你永远找不到你正在寻找的 sQuiz 的实例并且this.varName保持未定义(实际上,对象根本没有varName属性,除非您在其他地方以其他方式添加它)。
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.
您需要使用其他一些方法来引用实例——可能使用模块模式并在闭包中保留一个引用,或者每个实例的引用数组。
回答by tinyd
I'm not 100% sure, but I get a JavaScript error when I load the page in IE. This line:-
我不是 100% 确定,但是当我在 IE 中加载页面时出现 JavaScript 错误。这一行:-
if(window[v] instanceof sQuiz)
is failing - you should check window[v] for null first before instanceof
失败 - 您应该在 instanceof 之前先检查 window[v] 是否为 null