通过 PHP 解析 JavaScript 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3943198/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:47:00  来源:igfitidea点击:

Parse a JavaScript file through PHP

phpjavascript

提问by DiogoNeves

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.

我有一个 JavaScript 文件,我想在其中包含一些 php 代码。问题是我在 PHP 上有一些定义,我也想在 JS 上使用。

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?

有没有办法在 HTML 中包含一个 .js 文件,允许服务器首先使用 php 解释它(在下载到客户端之前)?

Thanks :)

谢谢 :)

回答by Cfreak

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

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

file.php你还需要输出正确的标头,输出任何东西之前,你应该具备以下条件:

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 :)

编辑:正如@Tony_A 指出的那样,它应该是application/javascript. 当我在 2010 年写这篇文章时,我认为这并不重要:)

回答by Peter Johnson

Create a php file called javascript-test.php

创建一个名为 javascript-test.php 的 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:

然后链接到您的 php,就好像它是一个 javascript 文件:

<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.

如果您需要 php 文件具有 .js 扩展名,这可以在您的服务器配置中实现。

回答by Pekka

Sure, most easily by making it a js.phpfile.

当然,最简单的方法是将其设为js.php文件。

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

不过,如果可能,请考虑替代方案:在包含外部脚本文件之前,将 PHP 定义提取到 JavaScript 中:

 <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.

这样,外部 JavaScript 仍然可以作为静态内容提供,不需要通过 PHP 运行。那是较少的资源密集型。

回答by Felix Kling

Not tested, but it probably works:

未经测试,但它可能有效:

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

Try to give the script .phpas file extension.

尝试将脚本.php作为文件扩展名。

回答by Matthew

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):

您也可以在服务器级别执行此操作。假设您正在使用 apache,您可以将此行添加到您的配置中(即使您的 .htaccess 也可以):

AddType application/x-httpd-php .js

AddType application/x-httpd-php .js

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

你也可以用 css 甚至普通的 ol' html 页面来做到这一点。

I'm sure other server software have similar capabilities.

我相信其他服务器软件也有类似的功能。

回答by egis

<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_onevariable will be available to all js files.

<script> global_one = '<?php echo $global_one; ?>';</script>快速示例 ;) 如果您<head>在所有其他 javascript 文件之前将其放在 html 中,则该global_one变量将可用于所有 js 文件。

回答by JAL

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

是的,只需编写一个输出 JavaScript 的 php 文件,然后像往常一样将其包含在您的页面中,例如

<?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.

客户端不关心脚本文件是否以 .js、.php 或其他格式结尾,只关心 mime 类型和内容。

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.

您还可以使用 Apache 指令(可能在 .htaccess 文件中)来告诉它将某个 .js 文件作为 PHP 处理,或者将 filename.js 的请求定向到 filename.php,尽管这不是必需的。