dongxianghuan3587 2014-12-15 15:36
浏览 44
已采纳

Javascript函数本身调用

I want to display a customized alert when i click a but if im logged in i want to redirect to another page. I have this.

        <div id="contentCalendar" class="col-md-3 images_1_of_4 text-center">
            <a><img onClick="show_alert();" id="rotateme" class="img-responsive img-circle center-block" src="web/images/calendaricon.png"/></a>
            <h4><a href="#">Check our last events</a></h4>

        </div>
        <div id="myalert" class="alert alert-error">
        <a href="#" class="close" data-dismiss="alert">&times;</a>
        <strong>Error!</strong> You must be logged in to see our calendar.
        </div>  
        <script>    
        $('#myalert').hide(); 
        //What happen if you want to enter the events without loggin.
        var logged_in = <?php echo ($logged_in); ?>;
        function show_alert()
        {
            if(logged_in==true)
            {
                window.location="timeline.php";
            }
            else
            {
                $('#myalert').show();
            }
        }
        </script>

For some reason works like a charm when Logged_in is true and i get redirected. But when im logged out it just doesnt show the alert even if i didnt press the button at all.

Any ideas?

  • 写回答

4条回答 默认 最新

  • dsdtumf776629385 2014-12-15 15:49
    关注

    Always, again, always, run everything you embed in JS through json_encode:

    var logged_in = <?php echo json_encode($logged_in); ?>;
    

    When you simply echo a falsy value in php, it echoes nothing, thus breaking your javascript.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料