Javascript 使用 htaccess 将 js/css 解析为 PHP 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10722375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse js/css as a PHP file using htaccess
提问by Val
Hi I have been trying so hard to do the following on htaccess but it does not seem to work.
嗨,我一直在努力在 htaccess 上执行以下操作,但似乎不起作用。
can someone out there help me?
有人可以帮助我吗?
AddType application/x-httpd-php .js
AddType application/x-httpd-php .js
I tried the php4,php5 version all the things I could find on google but I had no luck.
我尝试了 php4、php5 版本的所有我能在谷歌上找到的东西,但我没有运气。
what have I missed?
我错过了什么?
in my file.js
在我的 file.js 中
are the following...
以下是...
<?php
$(document).ready();//jquery/javascript
?>
I am expect a php fatal error but I see exactly the above as plain text.
我预计会出现 php 致命错误,但我将上述内容完全视为纯文本。
回答by Lee Kowalkowski
If you want to do this using just .htaccess configuration (who wants to set the headers in everyPHP file?):
如果您只想使用 .htaccess 配置来执行此操作(谁想在每个PHP 文件中设置标题?):
<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>
回答by Alex
Try:
尝试:
<FilesMatch "\.(js)$">
AddHandler application/x-httpd-php .js
</FilesMatch>
回答by Steve
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.
如果我是对的,那么您的 PHP JS 文件必须带有内容类型的标头:application/x-javascript,否则它不会被解释为 JS。
Header("content-type: application/x-javascript");
Header("content-type: application/x-javascript");
回答by Marcus Recck
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.
如果您试图让 PHP 在 Javascript 文件中工作,您可以简单地将 Javascript 重命名为javascript_file.js.php
并让 PHP 在其中工作。
You will have no problem including that into your page.
您将没有问题将其包含到您的页面中。
`
`
回答by Chris Mohr
One possibility is that you're not configured to allow the use of an .htaccess file.
一种可能性是您没有配置为允许使用 .htaccess 文件。
You can check this in your httpd.conf.
你可以在你的 httpd.conf 中检查这个。
You want to ensure that AllowOverride
is set to All
您要确保AllowOverride
设置为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>
回答by Michael O'Neal
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:
我对 .htaccess 了解不多,但我相信解决您的问题的部分方法是将 jQuery 移到 php 标签之外,如下所示:
<?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.
您的文件将被解析为任何其他 php 文件,因此您的 javascript 将在 javascript 标签内正常执行。
if you would like to include php variables inside your javascript just do as so:
如果您想在 javascript 中包含 php 变量,请执行以下操作:
<?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
上面的代码将返回 SPAN 元素内的当前纪元时间戳