Reputation: 1068
I have a function in my Javascript that causes errors in the function and in other functions as well, but the code compile just fine.If I remove the function, all of the red underlined errors are disappearing. I tried cleaning/rebuilding/restarting the computer but nothing changes. Here's the function
<script type="text/javascript">
function verifyRange(name, date, currentValue) {
switch (name) {
case "FC_OAT":
case "Forecast Air Temp. (°C)":
if (currentValue != null && currentValue < <%=ConfigurationManager.AppSettings["MinOAT"]%> || currentValue > <%=ConfigurationManager.AppSettings["MaxOAT"]%>) {
createAnnotation("Forecast Air Temp. (°C)","Forecast Air Temp. (°C)" + " \n<%=GetGlobalResourceObject(Session["culture"].ToString(),"OutOfBounds") %>\n[MIN:<%=ConfigurationManager.AppSettings["MinOAT"]%>, MAX:<%=ConfigurationManager.AppSettings["MaxOAT"]%>] \n<%=GetGlobalResourceObject(Session["culture"].ToString(),"ValueIs") %> " + currentValue + "°C",date);
}
break*;*
*case* "OAT":
case "Air Temp. (°C)":
if (currentValue != null && currentValue < <%=ConfigurationManager.AppSettings["MinOAT"]%> || currentValue > <%=ConfigurationManager.AppSettings["MaxOAT"]%>) {
createAnnotation("Air Temp. (°C)","Air Temp. (°C)" + " \n<%=GetGlobalResourceObject(Session["culture"].ToString(),"OutOfBounds") %>\n[MIN:<%=ConfigurationManager.AppSettings["MinOAT"]%>, MAX:<%=ConfigurationManager.AppSettings["MaxOAT"]%>] \n<%=GetGlobalResourceObject(Session["culture"].ToString(),"ValueIs") %> " + currentValue + "°C",date);
}
break;
default:
break;
}
}
The *;* gets error : can't have 'break' outside of loop
The *case* gets error : Expected '}'
After this function I have this code
$(document).ready(function *(*) {
alert('hello');
}*)*;
</script>
The *(* gets error : Expected identifier
The *)* gets error : Syntax error
Of course, the ** are used to show where the red underline is done by visual studio, I don't have this in my code.
My question is : Is there a way to have server tags inside an if without breaking visualstudio? Is it a bad way of coding to do so?
Upvotes: 0
Views: 116
Reputation: 2565
Try to refactor your code so the server values assigned to js vars and substitute them in the if statement:
var minOAT = <%=ConfigurationManager.AppSettings["MinOAT"]%>;
var maxOAT = <%=ConfigurationManager.AppSettings["MaxOAT"]%>;
var outOfBoundsString = "<%=GetGlobalResourceObject(Session["culture"].ToString(),"OutOfBounds") %>";
var valueIsString = "<%=GetGlobalResourceObject(Session["culture"].ToString(),"ValueIs") %>";
if (currentValue != null && (currentValue < minOAT || currentValue > maxOAT)) {
createAnnotation(
"Forecast Air Temp. (°C)",
"Forecast Air Temp. (°C) \n" +
outOfBoundsString + "\n" +
"[MIN:" + minOAT + ", MAX:" + maxOAT + "]\n" +
valueIsString + " " + currentValue + "°C",
date);
}
This should make the code look less messy and hopefully the error will disappear.
Upvotes: 2