无法从主体 onload 调用函数(未捕获的引用错误:未定义启动)javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31396502/
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
Can't call function from body onload (uncaught reference error: start is not defined) javascript
提问by Tae Hagen
I have a body onload calling a function in javascript. I Have tried many things, but the console just prints to the error log: uncaught reference error: start is not defined. I think it might be a malfunction, please notify me if it works for you. My code is as follows:
我有一个主体 onload 在 javascript 中调用一个函数。我尝试了很多东西,但控制台只是打印到错误日志:未捕获的引用错误:未定义启动。我认为这可能是故障,如果对您有用,请通知我。我的代码如下:
<!DOCTYPE html>
<html>
<head>
<title>Monster Invasion</title>
<script type="javascript">
var hur;
var monsters = 10;
var objective = false;
var health = 100;
var damage = 30;
var sheild = false;
var ea = 1;
function start() {
setTimeout(hurt,4000)
}
function hurt() {
var newhelth = health - damage;
health = newhelth;
document.getElementById("healtw").innerHTML = health;
start();
}
function kill() {
if(monsters > 0) {
monsters--;
document.getElementById("monster1").src="dead.jpg"
setTimeout(next,2000)
}else {
objective = true;
document.location="endoflevel.html";
}
}
function next() {
document.getElementById("monster1").src="monster.jpg"
}
}
</script>
</head>
<body onload="start()">
<p id="healtw"></p>
<embed src="guide_first_level.mp3" type="audio/mp3" hidden="true" autostart="true">
<a id="st" onclick="kill()"><img id="monster1" src="monster.jpg"></a>
<p id="ada"></p>
<a href="sheild.html">Activate sheild</a>
</body>
</html>
回答by Anik Islam Abhi
your script type is wrong
你的脚本类型错误
try like this
像这样尝试
<script type="text/javascript">
</script>
But you don't need to mention script type
但是你不需要提到脚本类型
just use plain script tag
只需使用普通脚本标签
like this
像这样
<script>
</script>
One more thing to add that before end of script tag you give extra }
.
还有一件事要在脚本标记结束之前添加额外的}
.
just remove it
删除它
function next() {
document.getElementById("monster1").src="monster.jpg"
}
} // <-- just remove this bracket
</script>
回答by Nancy256
Well for some of you, who are using external js file, please just check to see whether you linked your file or not :)... because I had the same error but as I looked through my code ..I hadn't linked the file then!
那么对于你们中的一些使用外部 js 文件的人来说,请检查你是否链接了你的文件:)...因为我有同样的错误但是当我查看我的代码时..我没有链接然后文件!
just in case you came here looking for an answer too!
以防万一你也来这里寻找答案!
回答by Maxim Gritsenko
There are two problems with your code.
您的代码有两个问题。
javascriptis not a valid type for the script in the browser. Browsers know
text/javascript
, but not just javascript, that is why your code is not being executed. Change the type attribute or remove it at all (fortext/javascript
is the default value).You have an extra bracket } at the end of the code. Remove it and your code will work fine.
javascript不是浏览器中脚本的有效类型。浏览器知道
text/javascript
,但不仅仅是javascript,这就是为什么您的代码没有被执行。更改 type 属性或完全删除它(fortext/javascript
是默认值)。代码末尾有一个额外的括号 }。删除它,您的代码将正常工作。
回答by leo.fcx
There are 3 errors:
有3个错误:
- Correct the
type
of your script tag - There is an extra
}
at the end of your code - Instead of using
onload()
forbody
tag, you should better usewindow.onload
as follows in order to make sure yourstart()
function is run once document is ready:
- 更正
type
你的脚本标签 }
您的代码末尾有一个额外的- 不要使用
onload()
forbody
标记,您最好使用window.onload
如下方法,以确保您的start()
函数在文档准备好后运行:
...
<head>
<title>Monster Invasion</title>
<script type="text/javascript">
window.onload = function(){
// Put all your code here
// And at the end run start() function
start();
}
</script>
</head>
<body>
...