Use PHP code in external Javascript file

前端 未结 5 408
甜味超标
甜味超标 2020-12-01 05:54

I am just wondering if it\'s possible to use external JS file that contains PHP code.

my external JS

$(document).ready(function(){

    $(\'#update\         


        
5条回答
  •  再見小時候
    2020-12-01 06:25

    You can do it by either renaming you js file to php and using this link in script tag src (as pointed in other answers)

    While this may seems a good and easy way there is many reason not to do this.

    • Caching (your generated script will not be cached, this may be changed but require additional work)
    • Memory consumption (every request to this generated js file will require php)

    You can simply changed to something like this to get it done (in a better way, IMO):

    
    
    

    Than content of your js became something like this:

    $(document).ready(function(){
      // I assume that not using search_city in your sample code
      // and some syntax errors, is just by mistake...
      $('#update').click(function(){
      var tableVal={search_city:Settings.search_city};
      $.post(Settings.base_url+'/project_detail/pub',
        {'tableVal':tableVal},
        function(message){
          console.log(message);
        })
      })
    })
    

提交回复
热议问题