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

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

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


  
    

        
7条回答
  •  名媛妹妹
    2021-02-15 13:30

    It makes it easy to set javascript variables from the server side.

    var foo = 
    

    I usually have one php/javascript file in my projects that I use define any variables that need to be used in javascript. That way I can access constants used on the server-side (css colors, non-sensitive site properties, etc) easily in javascript.

    Edit: For example here's a copy of my config.js.php file from the project I'm currently working on.

    
    

提交回复
热议问题