Inserting php into js file

前端 未结 5 1004
遇见更好的自我
遇见更好的自我 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:06

    I am not totally sure what you are trying to do. But if that JS isn't working, why not including a php file, or just writing some php in the header, that includes the JS inside it in 'echo()'. I.e:

     echo('?><img src="<?php bloginfo('url');?>/images/loading.gif" /><?php');
    

    Correct me if I am misunderstanding your intent.

    0 讨论(0)
  • 2021-01-15 02:14

    You can create a php file instead (of your js file with all the code you already have in that js file + references to your php variables/functions) and include that in your main php file.

    Example html:

    <?php $example = 23; ?>
    <html>
    <head><title></title>
    <script type="text/javascript">
    <?php include('js.php'); ?>
    </script>
    </head>
    <body></body>
    </html>
    

    js.php:

    var a = <?= $example ?>;
    alert(a);
    

    will eventually output:


    <html>
    <head><title></title>
    <script type="text/javascript">
        var a = 23;
        alert(a);
    </script>
    </head>
    <body></body>
    </html>
    
    0 讨论(0)
  • 2021-01-15 02:18

    I prefer to..

    1) In my header include (the php include that contains any <head> data), write a small inline JS function that creates a global object containing any variables I need from the server (PHP). I can use the 'echo' and 'json_encode' functions here because its in an inline JS snippet in a php file.

    2) You could write a JS function inside your JS file that uses AJAX to call a PHP file, which will return the same JSON encoded data as number 1, then you parse it and assign in to a global variable.

    Both essentially do the same thing, except with 2 you are using AJAX, which will add an additional request. I prefer option 1, because it is done along with the pages execution. Option 2 may require you to wait until the DOM is ready, depending on various aspects of your program (in which I can not tell).

    Option 1 requires inline JS, but you really shouldn't harp on this, as with dynamic websites it can actually be a plus, as along as it is short and concise. Some people get on others about inline JS. Don't let them yell at you.

    0 讨论(0)
  • 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:

    <body data-home-url="<?php echo home_url(); ?>">
    

    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.

    0 讨论(0)
  • 2021-01-15 02:32

    You can't place PHP code directly into a .js file, but you could have some javascript in the head element of your PHP file right before including the javascript file.

    In this javascript you could then define variables and assign data to them using PHP. These variables would then be accessible from inside the javascript file.

    <head>
        <script type='text/javascript'>
           var _g_bloginfo = "<?php echo '...'; ?>";
        </script>
    
        <script type='text/javascript' src='javascript.js'></script>
    </head>
    
    0 讨论(0)
提交回复
热议问题