I am looking for a solution in PHP that will allow me to reference a PHP file that outputs javascript as a script includes in the header of my html page. The reason i'm looking to do this is because I have some dynamically generated javascript which I want the browser to cache.
I have done this before in ASP.NET by using a .ashx handler, but i'm not sure how to do it in PHP.
Here's a more detailed breakdown of what i'm trying to achieve.
<!DOCTYPE html>
<html lang="en">
<script type="text/javascript" src="javascriptHandler.php"></script>
<head>
</head>
In the above example I want the javascriptHandler.php
to respond with a javascript file once it's requested.
The javascriptHandler.php
needs to check the incoming request headers and determine whether the file already exists on the client and return the appropriate response.
I'm looking for any clean solution that will do what is described above or any links that will point me in the right direction. If there's a better way to include and cache dynamically generated javascript please post it here.
<?php if( /* condition /* ) { ?> <script> //JAVASCRIPT </script> <?php } ?>
– ksbg Mar 2 '15 at 8:31javascriptHandler.php
(for cache-control and mime-type). – RobSky Mar 2 '15 at 9:24