Hi I have been trying so hard to do the following on htaccess but it does not seem to work.
can someone out there help me?
AddType application/x-httpd-
Try:
<FilesMatch "\.(js)$">
AddHandler application/x-httpd-php .js
</FilesMatch>
If you want to do this using just .htaccess configuration (who wants to set the headers in every PHP file?):
<FilesMatch "\.css$">
SetHandler application/x-httpd-php
Header set Content-type "text/css"
</FilesMatch>
<FilesMatch "\.js$">
SetHandler application/x-httpd-php
Header set Content-type "application/javascript"
</FilesMatch>
If I am correct, your PHP JS file has to be served with a header of content-type: application/x-javascript, otherwise it is not interpreted as JS.
Header("content-type: application/x-javascript");
I don't know much about .htaccess but I do believe part the solution to your problem is to move the jQuery outside of the php tags as so:
<?php
?>
</script>
$(document).ready();
<script>
your file is going to be parsed as any other php file, so your javascript will execute as normal, inside of javascript tags.
if you would like to include php variables inside your javascript just do as so:
<?php
$time=time();
?>
<script>
$( document ).ready(function(){
$("SPAN").text("<? echo $time; ?>");
});
</script>
<SPAN></SPAN>
the above code will return the current epoch timestamp inside of the SPAN element
If you're trying to make PHP work in Javascript files you can simply rename the Javascript to javascript_file.js.php
and have PHP work inside of that.
You will have no problem including that into your page.
`
One possibility is that you're not configured to allow the use of an .htaccess file.
You can check this in your httpd.conf.
You want to ensure that AllowOverride
is set to All
eg
from
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Satisfy all
</Directory>
to
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
Satisfy all
</Directory>