最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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 = ["wele_mat.html", ""];
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="";
document.getElementById("text2").focus();
</script>

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 = ["wele_mat.html", "http://www.youtube./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./user/1americanews";
document.getElementById("text2").focus();
</script>

Share Improve this question asked Jun 21, 2015 at 1:04 ColorCodinColorCodin 1012 silver badges11 bronze badges 3
  • 1 Well, you have a syntax error: Uncaught SyntaxError: Unexpected token else, hence the JavaScript code cannot execute. Hint: The ; after if() is very wrong. I suggest you have a look at the MDN JavaScript Guide to learn the basic syntax. Also note that there is no element with ID Frame. – Felix Kling Commented Jun 21, 2015 at 1:05
  • 1 What exactly is your problem? The code seems inplete. You're also missing semicolons at the end of several lines. – hamstu Commented Jun 21, 2015 at 1:06
  • 1 @hamstu Semicolons may be 'remended' but are optional (excluding a few cases of incorrect ASI application) and omissions will general not trigger syntax errors or different behavior. Now, extra semicolons between syntax constructs (as shown) cause the remainder of the syntax to be invalid - ie if (..); is valid (even if nonsensical) while if (..); {} else is a syntax error. – user2864740 Commented Jun 21, 2015 at 1:22
Add a ment  | 

3 Answers 3

Reset to default 3

there is a semi colon after your first if statement

replace

if (channel == 65);{

with

if (channel == 65){

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 = ["wele_mat.html", "http://www.youtube./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./user/1americanews";
    document.getElementById("text2").focus();
</script>

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 parison. Try putting your JS code in an external file.

发布评论

评论列表(0)

  1. 暂无评论