<script type="text/javascript">
onerror = unhandled;
function unhandled(msg, url, line) {
alert('There has been an unhandled exception.');
}
function add() {
alert($("#int1").val() + $("#int2").val());
}
function divide() {
if (isNaN($("#int1").val()) || isNaN($("#int2").val())) {
throw ('one or more values are non-numberic');
}
alert($("#int1").val() / $("#int2").val());
}
</script>
Integer 1: <input type="text" id="int1" /> <br />
Integer 2: <input type="text" id="int2" /> <br /><br />
<input type="button" name="add" value="Add" οnclick="add()" />
<input type="button" name="divide" value="Divide" οnclick="divide()" />
Question 1: if the user enters the number 2 in the int1 textbox, the number 3 in the int2 textbox, what does the webpage displays?
Question 2: If the user enters the number 2 in the int1 textbox, the number 0 in the int2 textbox, what does the webpage displays?
Question 3: If the user enters the number 2 in the int1 textbox, the letter a in the int2 textbox, what does the webpage displays?
Answer:
Q1, 23. 原因:js中得到的值都是string类型的,所以直接相加会得到 23,要想得到正确的值,需要改成 alert(parseInt($("#int1).val()) + parseInt($("#int2).val()));
Q2, infinity, 原因,在js中 除以0,得到是 infinity,即无穷大,而不会报异常。
Q3, There has been an unhandled exception. 原因,a不是数字,会throw('one or more values are non-numberic');
然后,就调用unhandled函数,抛出one or more values are non-numberic