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

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

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


  
    

        
相关标签:
7条回答
  • 2021-02-15 13:18

    If you don't need it, don't use it:

    The first thing you need to keep in mind is YAGNI. You Ain't Gonna Need It. Until a certain feature, principle, or guideline becomes useful and relevant, don't use it.

    Disadvantages:

    • Added complexity
    • Slower than static files.
    • Caching problems (server side)
    • Scalability issues (load balancers offload static files from the heavy PHP/Apache etc processes)

    Advantages:

    • User specific javascript - Can be achieved by initializing with the right variables / parameters in the <head> </head> section of the HTML
    • Page specific javascript - JS could also be generalized to use parameters
    • JSON created from database (usually requested via AJAX)

    Unless the javascript is truely unique (i.e. JSON, parameters/variables) you don't gain much. But in every case you should minimize the amount of JS generated on the server side and maximize the amount of code in the static files. Don't forget that if it's dynamic, it has to be generated/downloaded again and again so it's not wanted for it to be a heavy process.

    Also:

    • This could also be used to minimize the amount of server configuration (for example if the web server doesn't serve file.js with the correct content type)
    0 讨论(0)
  • 2021-02-15 13:20

    If you don't have full server access and can't turn on gzip encoding then it's pretty useful to put the following in your javascript file (note: will need to be renamed to file.js.php or parsed as PHP through .htaccess directive):

    <?php
    ob_start( 'ob_gzhandler' );
    header("Content-type: text/javascript");
    ?>
    // put all your regular javascript below...
    

    You could also use it for better cache control, visitor tracking, etc in lieu of server-controlled solutions.

    0 讨论(0)
  • 2021-02-15 13:25

    Advantage (not PHP specific - I used this technique in EmbPerl and JSP) would be the ability to dynamically generate or tweak/customize the JavaScript code on the server side.

    An example usage would be population of an array based on the contents of a DB table. Or application of localization techniques.

    0 讨论(0)
  • 2021-02-15 13:25

    Absolutely none, IMHO. I use a js framework that I wrote to handle the setting of whatever server-side variables I need to have access to. It is essentially the same as embedding PHP in JavaScript, but much less ambiguous. Using this method allows you to also completely separate server-side logic and html away from javascript. This results in much cleaner, more organized and lowly-coupled modular code.

    You could do something like this in your html:

    <script type="text/javascript">
    
    registry = {
        myString : '<?php echo $somePhpString; ?>',
        myInt    : <?php echo $somePhpInteger; ?>
    }
    
    </script>
    

    And then do something like this in your js:

    if (registry.myInt === 1) {
        alert(registry.myString);
    }
    
    0 讨论(0)
  • 2021-02-15 13:27

    There's no benefit for the example you gave above (beyond peculiar deployment scenarios where you have access to .php files and not .js files, which would be insane but not unheard of).

    That said, this approach allows you to pass the JS through the php parser - which means you can generate your JS dynamically based on server variables.

    0 讨论(0)
  • 2021-02-15 13:30

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

    var foo = <?=$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.

    <?php
    require_once    "libs/config.php";
    if (!function_exists("json_encode")) {
        require_once    "libs/JSON.php";
    }
    header("Content-type: text/javascript");
    echo "var COLORS = ". json_encode($CSS_COLORS) .";\n";
    echo "var DEBUG = ". ((DEBUG == true) ? "true" : "false").";";
    
    
    ?>
    
    0 讨论(0)
提交回复
热议问题