doufang7385 2016-12-01 18:33
浏览 32

仅在满足php条件时运行javascript

How do you run javascript if only a php condition is met? I am trying to show a pop-up only if $page_count is <= 1. The php is fine and runs correctly as I have tested it with simple echos, it is only when I try to negate the javascript from running if the $page_count is over 2 that this still runs.

Is there a common method to go about this?

if ($page_count <= 1) {
    $setup_popup =
    '<div id="status-popup">
        <div id="status-popup-container" class="total-center">
            <a class="popup-close" data-popup-close="popup-1" href="#">Close</a>
        </div>
    </div>'
    ;
}
else {
    $setup_popup = NULL;
}

Javascript

$('#status-popup').fadeIn(350);

$('[data-popup-close]').on('click', function(e)  {
    var targeted_popup_class = jQuery(this).attr('data-popup-close');
    $('#status-popup').fadeOut(350);
    $('body').css('overflow', 'auto');
     e.preventDefault();
});
  • 写回答

4条回答 默认 最新

  • dtj2ww9500 2016-12-01 18:38
    关注

    As the commenters suggested, something as simple as:

    if(document.getElementById('status-popup')){
        $('#status-popup').fadeIn(350);
    
        $('[data-popup-close]').on('click', function(e)  {
            var targeted_popup_class = jQuery(this).attr('data-popup-close');
            $('#status-popup').fadeOut(350);
            $('body').css('overflow', 'auto');
             e.preventDefault();
        });
    }
    

    should do it.

    You could also enclose the javascript inside a PHP if statement if necessary:

    <?php
    if ($page_count <= 1) {
    ?>
            $('#status-popup').fadeIn(350);
    
            $('[data-popup-close]').on('click', function(e)  {
                var targeted_popup_class = jQuery(this).attr('data-popup-close');
                $('#status-popup').fadeOut(350);
                $('body').css('overflow', 'auto');
                 e.preventDefault();
            });
    <?php
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料