External javascript with php question

just out of curiosity, what are the disadvantages of calling an external JavaScript file but having the file type as .php


<script type="text/javascript" src="example.php"></script>

Since I can just put something like

Header("content-type: application/x-javascript");

to have it render as JavaScript.

Is this a “good” or “bad” practice?

AFAIK browsers won’t balk at it. The only possible problem I can think of is that some may be able to see that its a generated file and maybe exploit the fact?

Of course you could do some htaccess rewrite stuff so that requests for myphpjavascriptfile.js go to myphpjavascriptfile.php and nobody would be the wiser.

You may want to use some expire nocache headers too.

Cool, I just really dislike it when I have to put the actual script on the page it self >_<. So if I avoid using http queries like $_GET I should be fine?

Thanks Mittineague

I’ve used both $_GET and $_POST to fetch JSON without any problems. Just be careful to validate and sanitize as should be the case always anyway.

I do a lot of my CSS and js includes that way so I can use php variables inside the scripts or stylesheets. No measurable downside.