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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探