在什么条件下我可以将PHP放入Javascript代码中?总是好的吗?这只是一个坏主意吗?我很惊讶我还没有看到它。
Under what conditions can I put PHP into Javascript code? Is it always ok? Is it just a bad idea? I am surprised I have not seen it more.
例如,我遇到过这个..
For example, I came across this..
$('#thumbnail').imgAreaSelect({aspectRatio: '<?php echo $thumb_height/$thumb_width;?>', onSelectChange:preview});..这是一个onpage脚本元素。我不是那么有经验,但我很惊讶我之前从未见过混合js / php。但是你也可以把PHP放在.js文件中吗?如果我将文件扩展名设置为.php,是否会解析文件?
..which was in an onpage script element. I'm not so experienced, but I was just surprised that I had never seen mixed js/php before. But can you put PHP in .js files as well? If I set the file extension to .php, will the file be parsed?
推荐答案您可以将PHP代码放入 * .js 文件但你需要告诉网络服务器也在 *。js 文件上执行PHP解释器。这样的东西应该在你的 httpd.conf
You can put PHP code into *.js files but you need to tell the webserver to also execute the PHP interpreter on *.js files. Something like this should be in your httpd.conf
<IfModule php5_module> AddType application/x-httpd-php .php .php5 .js ... </IfModule>更好的解决方案是重命名 *。js 文件到 * .js.php 或只是 * .php 。浏览器不介意,PHP解释器也将在那些上执行。
A better solution is to just rename your *.js files to *.js.php or just *.php. The browsers won't mind and the PHP interpreter will also get executed on those.
更多推荐
混合php和javascript
发布评论