My code includes a simple script that is supposed to display one of three divs while hiding the other two. The function for this action is called using the following syntax:
onchange="switch(this);"
However, I encountered an error message in Firebug indicating a problem with this code:
Javascript Error: missing { before switch body
The error points to line one of my .php file where the doctype is declared as follows:
<!doctype html>
Interestingly, I have another page with the same doctype and similar script that works perfectly fine. The difference between the two pages lies in how the script is invoked.
Another observation from Firebug's output: On the functional page, the javascript snippet displayed by Firebug looks like this:
function onclick(event) {
switch(this);
}
On the non-functional page, however, Firebug does not show any relevant output related to onchange or onclick events. It simply displays the code from my javascript file and suggests that there is a missing opening bracket in the function, even though it is clearly present. This discrepancy might suggest a scope issue causing the defined function to be invisible to the calling elements. Any thoughts?
1: Why does Firebug indicate an error on line 1 where the doctype is declared, when the problematic function is not located in the same file?
2: Can the doctype influence the execution of Javascript, and how can I troubleshoot if it does?
I would like to stick to using HTML5 exclusively for this project and include a separate javascript file for backward compatibility. Any assistance would be greatly appreciated!
P.S. My system setup includes Ubuntu 11.10 with Apache2, PostgreSQL, and PHP5. Everything functions flawlessly except for this particular javascript issue.
EDIT: I had a momentary lapse, but these things happen. As pointed out in the responses, "switch" is a reserved keyword in Javascript, and changing the function name resolved the issue. In hindsight, I should have recognized this given that my text editor highlights keywords in brown...
I am leaving this post as-is (unless advised otherwise) in case someone else encounters a similar obstacle. The answer provided first will receive credit since it also explains why I received the error messages, which could be more beneficial in the long term than just recognizing switch statements.