"Function is undefined" error when dealing with defined functions in Javascript

5.3k Views Asked by At

I'm trying to use functions after taking a break from JavaScript (due to it giving me much grief with its syntax) and it again has decided to treat me brutally again, ignoring my functions.

<script type="text/javascript">
channel = 1
channel_array = ["welcome_mat.html", "http://www.youtube.com/user/1americanews"];
function Oooh(e){
 var unicode=e.keyCode? e.keyCode : e.charCode
 alert(unicode);
 if (unicode == 38);{
  alert("You hit the up button.");
  if (channel == 65);{
   channel = 1;
   document.getElementById("Frame").src = channel_array[channel]
  }
  else{
   channel = channel + 1;
   document.getElementById("Frame").src = channel_array[channel]
  }
 }
}
</script>
<input id="text2" type="text" size="2" maxlength="1" onkeyup="Oooh(event); this.select()" />
<script type="text/javascript">
document.getElementById("Frame").src="http://www.youtube.com/user/1americanews";
document.getElementById("text2").focus();
</script>

3

There are 3 best solutions below

0
On BEST ANSWER

You mentioned that you have trouble with JavaScript's syntax, which your code does.

The corrected version is this:

<script type="text/javascript">
    var channel = 1;
    var channel_array = ["welcome_mat.html", "http://www.youtube.com/user/1americanews"];
    function Oooh(e) {
        var unicode=e.keyCode ? e.keyCode : e.charCode;
        alert(unicode);
        if (unicode == 38) {
            alert("You hit the up button.");
            if (channel == 65) {
                channel = 1;
                document.getElementById("Frame").src = channel_array[channel];
            }
            else {
                channel = channel + 1;
                document.getElementById("Frame").src = channel_array[channel];
            }
        }
    }
</script>
<input id="text2" type="text" size="2" maxlength="1" onkeyup="Oooh(event); this.select()">
<script type="text/javascript">
    document.getElementById("Frame").src = "http://www.youtube.com/user/1americanews";
    document.getElementById("text2").focus();
</script>
0
On

there is a semi colon after your first if statement

replace

if (channel == 65);{

with

if (channel == 65){
0
On

There are a couple of errors in your script, caused by missing and/or invalid tokens/semicolons.

It should look like this:

function Oooh(e) {
    var unicode = e.keyCode ? e.keyCode : e.charCode;
    alert(unicode);
    if (unicode === 38) {
        alert("You hit the up button.");
        if (channel === 65) {
            channel = 1;
            document.getElementById("Frame").src = channel_array[channel];
        } else {
            channel = channel + 1;
            document.getElementById("Frame").src = channel_array[channel];
        }
    }
}

The main problems are the ; after your if-statements.

Please also note: Using semicolons at the end of the appropriate lines is good coding style in JS. Use === instead of == to ensure type-safe comparison. Try putting your JS code in an external file.