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;
afterif()
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 IDFrame
. – 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) whileif (..); {} else
is a syntax error. – user2864740 Commented Jun 21, 2015 at 1:22
3 Answers
Reset to default 3there 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.