开发者

Javascript - Why is alert() ignored in my program

开发者 https://www.devze.com 2023-02-25 01:06 出处:网络
I\'m just learning javascript using a book and they give this example: <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”

I'm just learning javascript using a book and they give this example:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”开发者_开发技巧>
<body>
<script language=”JavaScript” type=”text/javascript”>
var firstNumber = 15;
var secondNumber = 10;
var answer;
answer = 15 / 10;
alert(answer);
alert(15 / 10);
answer = firstNumber / secondNumber;
alert(answer);
</script>
</body>
</html>

When I load it, alert only pops up twice instead of three times. Why is this?


The quotes you were using are doing something wierd and preventing the code to run. Wasn't getting any error just ignored...

Anyway try this working fine in FF, Chrome and Safari:

<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<script language="JavaScript" type="text/javascript">
var firstNumber = 15;
var secondNumber = 10;
var answer;
answer = 15 / 10;
alert("1: " + answer);
alert("2: " + 15 / 10);
answer = firstNumber / secondNumber;
alert("3: " + answer);
</script>
</body>
</html>


Probably because you've not copied the code exactly and somewhere in the real code you have a syntax error, or possibly a non-printable control character. You code is then crashing before it reaches the third alert. Check the error console - it should show up there (I assume you're using Firefox. which imho tends to be best for debugging Javascript).

0

精彩评论

暂无评论...
验证码 换一张
取 消