PHP - 生成 JavaScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5152055/
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
PHP - generating JavaScript
提问by Cleankod
I am working on a project which has a lot of JavaScript. I think that generating simple strings and putting them between "<script>" tags is not the best way to do it.
我正在开发一个包含大量 JavaScript 的项目。我认为生成简单的字符串并将它们放在“<script>”标签之间并不是最好的方法。
Are there any better approaches to generate JavaScript objects, functions, calls etc? Maybe some convering classes (from PHP to JavaScript) or maybe there are design patterns I should follow?
有没有更好的方法来生成 JavaScript 对象、函数、调用等?也许一些转换类(从 PHP 到 JavaScript)或者我应该遵循一些设计模式?
PS. If it has any relevance - I am using MooTools with MochaUI (with MochaPHPControl).
附注。如果它有任何相关性 - 我将 MooTools 与 MochaUI(与 MochaPHPControl)一起使用。
Thanks in advance for the help.
在此先感谢您的帮助。
回答by Ivan
The best approach is to think Javascript as PHP code.
最好的方法是将 Javascript 视为 PHP 代码。
The basic steps are:
基本步骤是:
- Create a .htaccess file that redirects all JS files (.js) to a index file.
- In this index file load JS file as a PHP module (
require "script/$file") - Modify all JS files as you need. Now you can embed PHP code.
- 创建一个 .htaccess 文件,将所有 JS 文件 (.js) 重定向到一个索引文件。
- 在此索引文件中加载 JS 文件作为 PHP 模块 (
require "script/$file") - 根据需要修改所有 JS 文件。现在您可以嵌入 PHP 代码。
For instance, add to your .htaccess this line:
例如,将这一行添加到您的 .htaccess 中:
RewriteRule \.js$ index.php
In your index.php file put something likethis:
在您的 index.php 文件中放置如下内容:
// Process special JS files
$requested = empty($_SERVER['REQUEST_URI']) ? "" : $_SERVER['REQUEST_URI'];
$filename = get_filename($requested);
if (file_ends_with($requested, '.js')) {
require("www/script/$filename.php");
exit;
}
And finally in your file.js.php you can embed PHP, use GET and POST params, etc:
最后在你的 file.js.php 中你可以嵌入 PHP,使用 GET 和 POST 参数等:
<?php header("Content-type: application/x-javascript"); ?>
var url = "<?php echo $url ?>";
var params = "<?php echo $params ?>";
function xxxx()....
....
In addition, a trick to skip file cache is to add a random number as javascript parameter:
此外,跳过文件缓存的一个技巧是添加一个随机数作为 javascript 参数:
<script type="text/javascript" src="scripts/file.js?a=<?php echo rand() ?>"></script>
Also, as said in a comment, if your webserver don't allow to modify the .htaccess, you can just ask for the PHP file directly:
此外,如评论中所述,如果您的网络服务器不允许修改.htaccess,您可以直接询问 PHP 文件:
<script type="text/javascript" src="file.php"></script>
回答by redcap3000
I was trying to make my server do an API call and embed it in a html to have javascript process it.. of course put whatever you want in the echo..
我试图让我的服务器执行 API 调用并将其嵌入到 html 中以让 javascript 处理它..当然把任何你想要的放在回声中..
<?php
// a_local_file.php
Header("content-type: application/x-javascript");
echo file_get_contents('http://some_remote_script');
?>
And use this to refer to it in HTML...
并使用 this 在 HTML 中引用它...
<script type="text/javascript" src="a_local_file.php"></script>
The api call should return valid javascript, otherwise add what you need to make it valid; this is often the troublesomepart with working with json from api calls. Stripslashes, trim, and other string functions may be of use .. but often pesky foreign characters seep in and can set off php errors based on your php setup.
api 调用应返回有效的 javascript,否则添加您需要的内容以使其有效;这通常是通过 api 调用使用 json 的麻烦部分。Stripslashes、trim 和其他字符串函数可能有用.. 但通常讨厌的外来字符会渗入并根据您的 php 设置引发 php 错误。
回答by Felix
You might want to check out json_encodeand json_decode.
您可能想查看json_encode和json_decode。

