Javascript外部文件函数赢得'我以前不工作

Javascript external file functions won't work for me, were working before

本文关键字:工作 文件 外部 函数 Javascript      更新时间:2023-09-26

第一次发帖,所以提前为我犯的任何错误道歉。当我在Chrome/Firefox中运行此代码时,我会收到要求输入数字的初始提示框。我输入了一个数字,什么也没发生。这以前是有效的,但当我回来检查代码中的另一个文件是否存在类似问题时,它不会打开第二个警报框。

Task2.html文件

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src ="task2.js"></script>
    </head>
    <body>
        <script>
            var tempSelect = 0;
            while (tempSelect != 3) {
                tempSelect = prompt("1.Fahrenheit to Celcius'n2.Celcius to Fahrenheit'n3.To Exit");
                if (tempSelect == 1) {
                    Cel();
                }
                else if (tempSelect == 2) {
                    Fahr();
                }
            }
            </script>
    </body>
</html>

Task2.js文件

  function Cel() {
    check = true;
    var fahr = prompt("Enter the degree in Fahrenheit to convert to Celcius");
    parseFloat(fahr);
    while (check == isNaN(fahr)) <!--isNaN  = is Not a number--><!--looping till a valid number is entered-->
    {
        alert("Enter a correct number!");
        fahr = prompt("Enter the degree in Fahrenheit to convert to Celcius");
    }
    var cel = ((5.0 / 9.0) * (fahr - 32)); <!--calculations -->
    parseFloat(cel);
    alert("Fahr to Cel ===> " + cel); <!--Output-->
}
function Fahr() {
    check = true;
    var cel = prompt("Enter the degree in Celcius to convert to Fahrenheit");
    parseFloat(fahr);
    while (check == isNaN(cel)) {
        alert("Enter a correct number!");
        var cel = prompt("Enter the degree in Celcius to convert to Fahrenheit");
    }
    var fahr = (9.0 / 5.0 * cel) + 32;
    alert("Cel to Fahr ===> " + fahr);
}

啊!我们都忽略了显而易见的一点:js文件中有HTML注释,这会导致javascript失败。

在javascript中使用/* comment */// comment而不是<!-- comment -->