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?
-
2"tried different things" ... always show what you triedcharlietfl– charlietfl2017年01月18日 23:35:54 +00:00Commented Jan 18, 2017 at 23:35
4 Answers 4
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()));
1 Comment
toLowerCase is a function ...also check spellingIf 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
<%=this.IsFull%> will be "True" or "False" - nothing elseTrue or False. If he can't depend on that, then he may need to do some string manipulation.'<%=this.IsFull%>'.toLowerCase() === 'true';Alternative method is
window.MyWIndow.IsFull = <%=this.IsFull.ToString().ToLower()%>;
Note, no quotes
Comments
wrong answer is wrong...
A couple of options. You can use the built in
Booleanobject 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]
4 Comments
myself.IsFull is a string - 'True' or 'False' as stated in the questionnew 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 truthyundefined - 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 falseExplore related questions
See similar questions with these tags.