2

I have a public property returned from my code-behind class to aspx page:

window.MyWIndow.IsFull = '<%=this.IsFull%>';

In code-behind it is defined this way:

public bool IsFull
{
    get;
    set;
}

Now, when I use it in my javascript file, I have the following code:

var isShow = myself.IsFull;

This way isShow is either 'True' or 'False'

I need to convert it on the page level, so isShow is boolean instead of string.

So I can write the if else logic

How can I do it?

1
  • 2
    "tried different things" ... always show what you tried Commented Jan 18, 2017 at 23:35

4 Answers 4

10

You can use JSON.parse('true');

JSON.parse(isShow.toLowerCase());

Try the example below.

var result = ['True', 'False']

var isShow = result[Math.round(Math.random())];

console.log(JSON.parse(isShow.toLowerCase()));

Sign up to request clarification or add additional context in comments.

1 Comment

toLowerCase is a function ...also check spelling
3

If you know it's always going to return the string True and False, you could just use;

window.MyWindow.IsFull = '<%=this.IsFull%>' === "True";

5 Comments

What if some browser returns "true" instead of "True"?
@gene - that's not up to the browser ... <%=this.IsFull%> will be "True" or "False" - nothing else
This this would assume you meant False. It's not the browser that returns this - it's his backend. He stated in the question very clearly that he knew it was going to return True or False. If he can't depend on that, then he may need to do some string manipulation.
@shadow - that was the OP asking!!
Huh, so it was. :P If your isFull function is returning a legit boolean, then it won't change case on you. But to be safe, you could always either use Jaramoda's idea, or '<%=this.IsFull%>'.toLowerCase() === 'true';
0

Alternative method is

window.MyWIndow.IsFull = <%=this.IsFull.ToString().ToLower()%>;

Note, no quotes

Comments

-1

wrong answer is wrong...

A couple of options. You can use the built in Boolean object wrapper like this:

var string = myself.IsFull
var isShow = new Boolean(string.toLowercase);

or use the logical NOT operator twice like this:

var string = myself.IsFull
var isShow = !(!string.toLowercase);

edit: but why???? [![enter image description here][1]][1]

[1]: https://i.sstatic.net/359zg.png

4 Comments

wont work if myself.IsFull is a string - 'True' or 'False' as stated in the question
fiiiiixed it :)
toLowerCase is a function - and new Boolean('false') is still true, as is !(!'false') - I suggest when you test your code, test that all possible inputs produce the desired output - clearly you're testing true only! - a non-empty string will always be truthy
because toLowercase is undefined - or falsey, any string given that code would result in false ... toLowerCase, on the other hand, is a function, using toLowerCase would result in any string becoming true - used as a function, toLowerCase() would result in any non-empty string being true, and empty string being false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.