admin管理员组文章数量:1390767
I'm trying to make a button appear if condition "isauthenticated" is "true".
used this as a reference, but not sure what's wrong with my code.
<button id="authentic" type="hidden">test</button>
<script>
window.onload = function() {
var logedIn = ('{{isauthenticated}}')
if(logedIn == "true") {
document.getElementById('authentic').style.display = 'block';
} else {
document.getElementById('authentic').style.display = 'none';
}
}
</script>
I'm trying to make a button appear if condition "isauthenticated" is "true".
used this as a reference, but not sure what's wrong with my code.
<button id="authentic" type="hidden">test</button>
<script>
window.onload = function() {
var logedIn = ('{{isauthenticated}}')
if(logedIn == "true") {
document.getElementById('authentic').style.display = 'block';
} else {
document.getElementById('authentic').style.display = 'none';
}
}
</script>
Share
Improve this question
edited May 23, 2017 at 11:44
CommunityBot
11 silver badge
asked Nov 7, 2013 at 16:47
user2755801user2755801
531 gold badge3 silver badges9 bronze badges
3
-
10
What's
{{isauthenticated}}
? Set by the server? Ractive.js template variable? – Zeta Commented Nov 7, 2013 at 16:48 - 1 check logedIn against true and not "true" – Anto Jurković Commented Nov 7, 2013 at 16:50
- 2 Did you try to log to console logedIn value? – Adriano Repetti Commented Nov 7, 2013 at 16:50
3 Answers
Reset to default 3Ok Guys, sorry for the bad question. This is what I was looking for
<script>
var isAuth = ('{{ user.isauthenticated}}');
if (isAuth) true;
document.getElementById("menuProfile").style.display="block";
document.getElementById("showProfile").style.display="none";
</script>
if logedIn
is a boolean value you need to remove the "
's from the if(logedIn == "true") {
line
or just check:
if(logedIn) {...
Please refer to Boolean Javascript documentatiion:
If the Boolean object has no initial value, or if the passed value is one of the following:
0,-0,null,"",false,undefined,NaN the object is set to false. For any other value it is set to true (even with the string "false")!
see what value does logedIn have before the if!
本文标签: javascriptIf condition is true show buttonStack Overflow
版权声明:本文标题:javascript - If condition is true show button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744715147a2621358.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论