javascript中未捕获的引用错误

时间:2013-12-03 18:07:02

标签: javascript html reference

我在以下脚本中一直收到Uncaught Reference错误。 Firebug一直告诉我,当我尝试将其命名为onLoad时,未定义“Initialize”。有人可以帮我发现我的错误吗?我已经看了好几个小时了,似乎无法找到它。

<html>
<head>
<script LANGUAGE="text/javascript">
var thePest

function VirtualHamster(state, petaction, petsound) {
    this.state = state
    this.petaction = petaction
    this.petsound = petsound
}

function Initialize() {
    thePest = new VirtualHamster("Sleeping", "", "Zzzzzzzz……")
    Display(thePest)
    setTimeout("Simulate(thePest)", 1000)
}

function Display(pest) {
    var state = pest.state
    if (state == "Excited") {
        pest.petaction = ""
        pest.petsound = "Eep eep!"
    } else if (state == "Sleeping") {
        pest.petaction = ""
        pest.petsound = "Zzzzzzzz……"
    } else if (state == "Bored") {
        pest.petsound = ""
        pest.petaction = "Stare at Owner"
    } else if (state == "Thirsty") {
        pest.petsound = ""
        pest.petaction = "Stick tongue out"
    } else if (state == "Hungry") {
        pest.petsound = ""
        pest.petaction = "Bite!!"
    } else {
        pest.petaction = "Error - unknown state"
        pest.petsound = "Oh No!"
    }

    document.getElementById("state").value = pest.state
    document.getElementById("petaction").value = pest.petaction
    document.getElementById("petsound").value = pest.petsound
}

function Scratch(pest) {
    var state = pest.state.value
    if (state == "Sleeping") {
        pest.state.value = "Excited"
    }
    Display(pest)
}

function Feed(pest) {
    var state = pest.state.value
    if (state == "Hungry") {
        pest.state.value = "Thirsty"
    }
    Display(pest)
}

function Simulate(pest) {
    var state = pest.state.value
    var n = Math.random()
    if (state == "Excited" && n < 0.2) {
        pest.state = "Bored"
    } else if (state == "Bored" && n < 0.25) {
        pest.state = "Hungry"
    } else if (state == "Thirsty" && n < 0.9) {
        pest.state = "Sleeping"
    }
    Display(pest)
    setTimeout("Simulate(thePest)", 1000)
}
</script>             
<body bgcolor="gold" onLoad="Initialize()">
<h1><center>Larry: The Virtual Hamster!</h1></center>
<center><img src="sleeping.jpg"></center>
<form NAME="pest">
<center><b>Pest Sound:</b> <input type=text name=petsound size=20 maxlength=30 /> </center><br>
<center><b>Pest Action:</b> <input type=text name=petaction size=20 maxlength=30 /></center> <br>
<center><b>The Pests state is:</b> <input type=text name=state size=20 maxlength=30 /></center> <br>
<center><input type="button" name="Scratch" value="Scratch Back"onClick="Scratch(thePest)"></center>
<center><input type="button" name="Feed" value="Feed the Hamster"onClick="Feed(thePest)"></center>
</form>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

你需要确保正确地使用逗号(,)和分号(;)来结束行。看起来你会遇到以var thePest开头的问题...一眼就看出,它似乎没有被用来定义VirtualHamster方法所以你需要在thePest之后使用一个逗号,例如

确保您正确格式化,否则您的定义将无法正确对齐,可以这么说。这被讨论为可选的。

无论哪种方式,您还需要在setTimeout中传递对Simulate的引用。只需说setTimeout(Simulate, 1000)而不是在Simulate中使用有害生物,使用全局的thePest

注意到您还需要为输入标记添加适当的ID属性。

<input type=text name=petsound size=20 maxlength=30 />变为<input id="petsound" type=text name=petsound size=20 maxlength=30 />

在这里工作小提琴http://jsfiddle.net/ykL7g/7/

答案 1 :(得分:0)

您似乎正在使用已弃用的属性language并且应该使用type

我把麻烦的代码放到了codepen上,我得到了同样的错误。

示例:

http://codepen.io/anon/pen/JjFEA

但如果我改变了

<script LANGUAGE="text/javascript">

<script type="text/javascript">

我能够运行Initialize()

示例:

http://codepen.io/anon/pen/djcJH

不推荐使用language属性,建议不要使用type,而应使用{{1}}。 更多信息[script language attribute]