Inserting php into js file

前端 未结 5 1063
遇见更好的自我
遇见更好的自我 2021-01-15 01:40

I have a loading gif for the gallery on my website. In my js file I have this code to show the loader:

image: $(\"\"),         


        
5条回答
  •  忘掉有多难
    2021-01-15 02:21

    A cleaner technique for passing PHP data to JavaScript is to store the data in data attributes which you can then access via JavaScript. As a simple example:

    
    

    You can access that in jQuery like:

    var home = $('body').attr('data-home-url');
    

    FYI you can use json_encode to convert PHP object/arrays into a JSON objects which you can read via $.parseJSON or JSON.parse. WP's wp_localize_script can actually do this for you, but note that in that case it'll expose the converted data to the window.

提交回复
热议问题