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条)

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏