混合php和javascript

编程入门 行业动态 更新时间:2024-10-27 18:28:30
本文介绍了混合php和javascript - 在什么条件下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在什么条件下我可以将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

本文发布于:2023-10-16 11:34:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497441.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:php   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!