通过PHP解析JavaScript文件 [英] Parse a JavaScript file through PHP

查看:105
本文介绍了通过PHP解析JavaScript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

谢谢:)

推荐答案

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

file.php 你也会想要输出正确的标题,在输出任何内容之前你应该有以下内容:

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

header("Content-Type: application/javascript");

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

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

这篇关于通过PHP解析JavaScript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆