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

javascript - If condition is true show button - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 3

Ok 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!

发布评论

评论列表(0)

  1. 暂无评论