10

I have a JavaScript file where I would like to include some php code. The problem is that I have a few defines on PHP that I would like to use on JS as well.

Is there any way of including a .js file in HTML allowing the server to first interpret it (before downloading to the client) using php?

Thanks :)

DiogoNeves
  • 1,727
  • 2
  • 17
  • 36
  • You mentioned that a PHP include or something appears to be causing problems for you. Care to elaborate on that? – JAL Oct 15 '10 at 14:37
  • In the past I have actually have had coldfusion functions produce javascript functions. I found this particularly useful when I had a variable number of fields and variably named fields, and needed some browser based functionality. – Jay Oct 15 '10 at 14:39
  • @Alex That was me being stupid with a broken require_once ;) cheers – DiogoNeves Oct 15 '10 at 15:08

7 Answers7

33
<script src="/path/to/my/file.php"></script>

In file.php you'll also want to output the correct header, before outputting anything you should have the following:

header("Content-Type: application/javascript");

EDIT: As @Tony_A pointed out, it should be application/javascript. I don't think it mattered as much when I wrote this post in 2010 :)

Cfreak
  • 19,191
  • 6
  • 49
  • 60
  • great, I managed to fail a few times but my mistake hehe cheers – DiogoNeves Oct 15 '10 at 14:59
  • I just answered a question similar to this and this question was referenced in it. Saw you were 1 away from a Nice Answer badge, so I helped you out. :) – Michael Irigoyen Jan 27 '11 at 21:33
  • @Cfreak are you still able to use $_GET to fetch query values from the parent document? – xxstevenxo Jul 25 '15 at 19:18
  • According to the [IANA Registered MIME media type list](http://www.iana.org/assignments/media-types/media-types.xhtml) you should use the mime type `application/javascript`. – Tony_A Jul 31 '15 at 10:52
  • So I've been trying to use this sort of functionality but I keep getting the following error message in my console in chrome: Refused to execute script from 'myurl/script.php?param=bar' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled. I have the script called as Any thoughts or suggestions would be really appreciated! – itchyspacesuit Mar 24 '17 at 20:56
  • you need the `header` call in my post. You need to make sure that's the very first thing that's output in your script (Make sure there's nothing before the opening ` – Cfreak Mar 25 '17 at 01:31
12

Sure, most easily by making it a js.php file.

If possible, though, consider an alternative: Fetch the PHP defines into JavaScript before including the external script file:

 <script>
 define1 = <?php echo YOUR_DEFINE1; ?>
 define2 = <?php echo YOUR_DEFINE2; ?>
 </script>
 <script src="....."> // This script can now use define1 and define2

This way, the external JavaScript can still be served as a static content and doesn't need to be run through PHP. That is less resource intensive.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
12

Create a php file called javascript-test.php

<?php
header('Content-type: application/javascript');

$php = 'Hello World';
echo "alert('$php');";
?>

And then link to your php as if it was a javascript file:

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

If you need your php file to have a .js extension, that is possible in your server configuration.

Peter Johnson
  • 2,673
  • 22
  • 14
7

Not tested, but it probably works:

<script lang="text/javascript" src="path/to/your/script.php" ></script>

Try to give the script .php as file extension.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • this will only has a chance of working if your PHP code is valid javascript syntax. – Jay Oct 15 '10 at 14:34
  • It isn't working for me but I also noticed that I'm requiring another php inside that might be creating problems :) – DiogoNeves Oct 15 '10 at 14:34
  • Downvoting is ok, but an explanation would be fine :) – Felix Kling Oct 15 '10 at 14:34
  • Jay, that seems to be the case – DiogoNeves Oct 15 '10 at 14:34
  • 3
    @Jay: Why? It is the same as embedding PHP in HTML. Of course you have to wrap your PHP code in `` the rest should be untouched be the PHP interpreter... or I don't get your point. And the JS code that you produce via PHP should be valid. But **PHP code** itself can never be *valid* JS code... – Felix Kling Oct 15 '10 at 14:35
  • @Jay I'm really, really sure that Felix realizes that the PHP has to output valid JS. – JAL Oct 15 '10 at 14:36
  • 2
    @Jay incorrect: The PHP code needs to *generate* javascript syntax. If we're already splitting hairs – Pekka Oct 15 '10 at 14:37
7

You can do this on a server level too. Say you're using apache, you can add this line to your configuration (even your .htaccess will do):

AddType application/x-httpd-php .js

You could also do that with css or even plain ol' html pages.

I'm sure other server software have similar capabilities.

Matthew
  • 15,282
  • 27
  • 88
  • 123
  • This is not a good idea. Not only will it be slower (all your .js files will be parsed through PHP whether they contain code or not) but it could also be a security vulnerability. – Cfreak Mar 25 '17 at 01:35
1

<script> global_one = '<?php echo $global_one; ?>';</script> Quick example ;) If you put this in your html <head> before all other javascript files the global_one variable will be available to all js files.

egis
  • 1,404
  • 2
  • 11
  • 24
0

Yes, just write a php file that outputs the JavaScript and include it in your page as you normally would, like

<?php $f=40; ?>
<script type='text/javascript>
   var f=<?php echo $f;?>;
</script>

The client does not care if the script file ends in .js, .php or whatever, just about the mime type and the contents.

You could also use Apache directives, perhaps in a .htaccess file, to tell it to process a certain .js file as PHP, or direct requests for filename.js to filename.php, though it's not necessary.

JAL
  • 21,295
  • 1
  • 48
  • 66