Passing data from php to Jquery

前端 未结 6 2325
北恋
北恋 2020-12-21 19:44

I have all my javascript/jquery code in a file and include it into my page. Without having apache process js files as php what would be the best way to pass data from php to

6条回答
  •  情话喂你
    2020-12-21 20:03

    Instead of mis-using HTML for passing data (which has to be parsed) you should actually echo JavaScript code. For example, PHP could generate:

    
    

    As this is declared in a global scope of your page your script can actually access these variables.

    The only thing you have to look out for are race conditions. Your script will have to execute after the data has been defined. However, with a standard jquery on-dom-ready callback that is not a problem.

提交回复
热议问题