timmyRS timmyRS - 6 months ago 15
PHP Question

Load a PHP file like it was a JavaScript

I have a

script.js.php
file that I need to load like this

<script src="script.js"></script>


I did instruct the
.htaccess
-file to ignore if
.php
is missing and it's working well, but it doesn't


Refused to execute script from ... because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.


And it wouldn't be a question on SO, if I didn't try to Mime-Type to
application/javascript
. I could load it in using jQuery, but:


  • $.getScript()
    seems to be bugging

  • $.load()
    doesn't fulfill my purpose


Answer

As it didn't work in JavaScript, I now have to use this method, which works:

<script>
    <?php include "script.js.php"; ?>
</script>

This method will however take the user longer to download this page, because caching isn't really possible in-line.

I would still be open for better approaches.