du4373 2011-08-17 12:29
浏览 13
已采纳

根据条件将javascript写入页面

I need to remove some Javascript code based on a server side condition (PHP). What I currently have is a variable holding the Javascript code as text and based on the condition I either echo it or not. However, it's cumbersome to maintain. How do I go about doing it?

I also tried to use something like what follows, but it's not working.

<?php if(condition) { ?> <script> stuff here </script> <?php } ?>

I'm sorry for the formatting, I have no idea why the less-then sign is making the entire line disappear.

  • 写回答

5条回答 默认 最新

  • douji2283 2011-08-17 12:35
    关注

    The best way is to do something like this:

    <?php
      if ($something) {
    ?>
    <script>
      alert('hi')
    </script>
    <?php
      }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?