4

I am trying to compare a database (SQL) value (which is being returned correctly) to the boolean value 'true'. If the database bit value is = true then I want a div element to become visible, else stay hidden.

<script language="javascript">
    window.onload= function show_CTL() {
        if(<%=_CurrentUser.IsCTL%> == true){
            document.getElementById('CTL').style.visibility = "visible";
        } else{
            document.getElementById('CTL').style.visibility = "hidden";
        }
    }    
</script>

However I am getting the error, Javascript: 'True' is undefined.

I have tried many combinations of <%=_CurrentUser.IsCTL%> == 'true' or "true" or true or "True" or 'true' and even the === ... but all give me the same error message.

Any insights on how to resolve this issue will be greatly appreciated.

I have such comparisons successfully before with integer values such as:-

 window.onload= function show() {
    if(<%=_CurrentUser.RoleKey%> == 1 || <%=_CurrentUser.RoleKey%> == 2)
            document.getElementById('enr').style.visibility = "visible";
    else
            document.getElementById('enr').style.visibility = "hidden";
 }
Damien Pollet
  • 6,488
  • 3
  • 27
  • 28
Philo
  • 1,931
  • 12
  • 39
  • 77
  • 2
    What language is `<%=_CurrentUser.IsCTL%>` and what does it output? – j08691 Apr 16 '14 at 16:59
  • it outputs true or false (boolean) values. appropriately for currently logged in user. – Philo Apr 16 '14 at 17:00
  • @j08691, it is asp.net I suppose – Amit Joki Apr 16 '14 at 17:01
  • Try `if("<%=_CurrentUser.IsCTL%>" == "True")` or `if(<%=_CurrentUser.IsCTL.ToString().ToLower()%> == true)` – Musa Apr 16 '14 at 17:02
  • 3
    What dose the code look like when it's sent to the browser? You're probably getting `True == true`, hence your error. `True` is an undefined variable in JS, `true` is a boolean value... – Marc B Apr 16 '14 at 17:03
  • The lhs of your equal is inserting the four letters `True`. Try enclosing them in quotes to make it a string and comparing them to the string "True". – thebjorn Apr 16 '14 at 17:03
  • a tip: always enclose the jsp expr in quotes as "<%=_CurrentUser.IsCTL%>", since if it empty string then it will be if(==true) which is a wrong syntax and might fail in this scenario. – vivek_nk Apr 16 '14 at 17:03

4 Answers4

5

Do this:

if("<%=_CurrentUser.IsCTL%>" === "True")

<%=_CurrentUser.IsCTL%> is returning True. So wrap it with string and compare them instead. Notice the '===' instead of '=='.

Community
  • 1
  • 1
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
2

In

if(<%=_CurrentUser.IsCTL%> == true)

I think <%=_CurrentUser.IsCTL%> is getting evaluated to True before the code is seen by the browser.
The browser will see this as

if(True == true)

True does not make a lot of sense to the browser, thats why the error. For this true to be treated as a boolean, try one of this:

if(new Boolean('<%=_CurrentUser.IsCTL%>') == true)

or

if(new Boolean('<%=_CurrentUser.IsCTL%>'))
Nivas
  • 18,126
  • 4
  • 62
  • 76
  • That doesn't work the way you think it does... `(new Boolean("False")) ? "TRUE" : "FALSE"` evaluates to... `"TRUE"` – thebjorn Apr 16 '14 at 17:12
  • @thebjorn you are right. Confused Java's behaviour with Javascript's behaviour. – Nivas Apr 16 '14 at 19:50
2

This has gotten me before as well. ASP.NET will return True for a boolean which is true. You have to make it a string and then compare it to the string version == "True" in order to get a proper conditional statement.

Conversely, you could also just make a variable in javascript

var True = true;
Travis J
  • 81,153
  • 41
  • 202
  • 273
0

You need to convert your native boolean value to the string "true" before output. So, assuming ASP.NET MVC, I believe it looks like:

<%=_CurrentUser.IsCTL ? "true" : "false"%>
Chuck
  • 234,037
  • 30
  • 302
  • 389