Mark Mintoff My superpower is common sense

17Dec/101

Combining JavaScript or CSS Files (PHP Version)

Please refer to my previous post which is done in ASP.Net. And now, may I present to you, the PHP version in all it's hastily coded atrocity: To reference this, simply type in something akin to the following into your <head> tag:
<script type="text/javascript" src="include.php?type=js"></script>
Also: If you get weird characters such as these beauties; ï»¿ please ensure that the files you are saving (the javascript and css files) are encoded without BOM (Byte Order Mark).
VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
Comments (1) Trackbacks (0)
  1. Good stuff :)

    It appears that you’re using the same resource sets wherever include.php is being called from (i.e. all pages include the same js or css. If that is the case, you can probably speed things up a bit more by pre-packing the files before deployment, rather than building the file each time. I wrote a post about this many moons ago, hope it helps: http://karlagius.com/2009/01/10/size-does-matter/

    It’s also possible to get rid of the request parameter, type. That would be a good idea since it may confuse caches between you and your client, which will slow you down. You could easily use apache rewrite rules to achieve this; on the .Net front, you should be able to use handlers to achieve the same effect.

    Rewrite rules:

    RewriteRule ^include.css$ include.php [T=text/css,L]
    RewriteRule ^include.js$ include.php [T=text/javascript,L]

    Php code then becomes:

    $isJs = (strpos($_SERVER["REQUEST_URI"],'.css') === false);

    if ($isJs) {
    header("Content-Type: text/javascript;");
    // JS handling code goes here
    } else {
    header("Content-Type: text/css;");
    // CSS handling code goes here
    }

    You can then use the normal, boring way to request the resources:

    <link rel="stylesheet" href="include.css" type="text/css" />
    <script src="include.js" type="text/javascript"></script>

    Hope this helps :)


Leave a comment

No trackbacks yet.