What are the advantages to putting your Javascript in a .php file?

前端 未结 7 2479
独厮守ぢ
独厮守ぢ 2021-02-15 12:40

I occasionally come across pages where some Javascript is included via a PHP file:


  
    

        
相关标签:
7条回答
  • 2021-02-15 13:31

    Agree with tj111. Apart from what tj mentioned, I also found php-generated javascripts a great weapon to fight the browser's caching tricks. Not that long ago I was cursing the whole javascript for its being constantly cached by the browser. Refreshing the page helped me not, had to clear the whole cache in order to force the browser to reload the javascript files. As soon as I built a php wall in front of my javascripts:

    fake_js.php:

    <?php 
    
    header('Content-type:  text/javascript') 
    include('the_real_javascript.js');
    
    ?>
    

    A fresh new javascript would always show up at the client side. However this approach is obviously only good in the development phase, when it can save the developer quite some headache to have the correct javascript loaded in the browser. Of course when connecting to localhost, the penalty of repeatedly loading the same file is not as big.

    In a live web application/site client-side caching is welcome to reduce network traffic and overall server load.

    0 讨论(0)
提交回复
热议问题