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

Javascript comparing boolean value to True - Stack Overflow

programmeradmin5浏览0评论

I am trying to pare 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 bee 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 binations 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 parisons 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";
 }

I am trying to pare 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 bee 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 binations 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 parisons 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";
 }
Share Improve this question edited Apr 18, 2014 at 14:59 Damien Pollet 6,5983 gold badges30 silver badges28 bronze badges asked Apr 16, 2014 at 16:57 PhiloPhilo 1,98912 gold badges40 silver badges80 bronze badges 7
  • 2 What language is <%=_CurrentUser.IsCTL%> and what does it output? – j08691 Commented Apr 16, 2014 at 16:59
  • it outputs true or false (boolean) values. appropriately for currently logged in user. – Philo Commented Apr 16, 2014 at 17:00
  • @j08691, it is asp I suppose – Amit Joki Commented Apr 16, 2014 at 17:01
  • Try if("<%=_CurrentUser.IsCTL%>" == "True") or if(<%=_CurrentUser.IsCTL.ToString().ToLower()%> == true) – Musa Commented Apr 16, 2014 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 Commented Apr 16, 2014 at 17:03
 |  Show 2 more ments

4 Answers 4

Reset to default 5

Do this:

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

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

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%>'))

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 pare 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;

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"%>
发布评论

评论列表(0)

  1. 暂无评论