清除PHP生成的JavaScript和CSS的链接

时间:2021-09-29 10:07:41

Background: When generating HTML content with PHP or any such thing, it is possible to encapsulate links to JavaScript and CSS inside tags without actually having to include the CSS and JavaScript "in-line" with the rest of the content. All you have to do is create a link to the file.

背景:当使用PHP或任何此类事物生成HTML内容时,可以在标记内部封装JavaScript和CSS的链接,而无需实际包含CSS和JavaScript与内容的其余部分“内联”。您所要做的就是创建一个指向该文件的链接。

Example: {script type="text/javascript" src="./js/fooscript.js"}{/script}

示例:{script type =“text / javascript”src =“./ js / fooscript.js”} {/ script}

Question: The above approach does not work, however, if your PHP needs to dynamically generate some or all of your JavaScript code. Is there a way to have a clean "one-line" link as above, but still use dynamically-generated JavaScript?

问题:但是,如果您的PHP需要动态生成部分或全部JavaScript代码,则上述方法不起作用。有没有办法像上面那样拥有一个干净的“单行”链接,但仍然使用动态生成的JavaScript?

Obviously, one way to do it is to have PHP auto-generate the JavaScript and write that to a file; however that approach is undesirable for various reasons. I am wondering if there is an alternate trick to doing this that I have not thought of yet.

显然,一种方法是让PHP自动生成JavaScript并将其写入文件;然而,由于各种原因,这种方法是不受欢我想知道是否有一个替代技巧来做到这一点,我还没有想到。

4 个解决方案

#1


Put an .htaccess file in your /js/ folder and add the .js extension to PHP, like this:

将.htaccess文件放在/ js /文件夹中,并将.js扩展名添加到PHP,如下所示:

AddHandler application/x-httpd-php .js

In other words, have PHP parse all .js files as PHP files. So your scripts would really be PHP files on the server-side that output JavaScript. Do the same for stylesheets, only use the .css extension, obviously.

换句话说,让PHP将所有.js文件解析为PHP文件。所以你的脚本实际上是服务器端输出JavaScript的PHP文件。对样式表执行相同操作,显然只使用.css扩展名。

Note: I've never tried doing this in a separate .htaccess file. If it doesn't work, just put it into your global Apache config.

注意:我从未尝试在单独的.htaccess文件中执行此操作。如果它不起作用,只需将它放入您的全局Apache配置中。

#2


From my experience, rarely do you need to (and rarely should you) generate an entire script dynamically. For example, in javascript you may need to dynamically get some piece of data (like user info or settings) into javascript, but the rest of the script (classes/functions/DOM manipulations) is static across all users.

根据我的经验,您很少需要(并且很少应该)动态生成整个脚本。例如,在javascript中,您可能需要动态地将一些数据(如用户信息或设置)转换为javascript,但其余的脚本(类/函数/ DOM操作)在所有用户中都是静态的。

Typically in this case you would just want to put the dynamic stuff "inline", output dynamically from PHP and then include the js (the 95% that doesn't need dynamically generated) as an external script. The most obvious reason for this is caching the js/css.

通常在这种情况下,您只想将动态内容“内联”,从PHP动态输出,然后将js(不需要动态生成的95%)作为外部脚本包含在内。最明显的原因是缓存js / css。

Consider how reddit.com does it by looking at their source code for getting user data into javascript.

考虑一下reddit.com如何通过查看用于将用户数据获取到javascript的源代码来实现它。

var reddit = {
    /* is the user logged in */ logged: 'username',
    /* the subreddit's name (for posts) */ post_site: "",
    /* are we in an iframe */ cnameframe: false,
    /* this page's referer" */ referer: "",
    /* the user's voting hash */ modhash: 'lzbcszj9nl521385b7e075e9750ee4339547befc6a47fa01e6',
    /* current domain */ cur_domain: "reddit.com", ...
}

The rest of their js is found in external files.

其余的js可以在外部文件中找到。

#3


You could just use mod_rewrite to make certain php files be seen as CSS/JS

您可以使用mod_rewrite将某些php文件视为CSS / JS

e.g. /css/screen-style.css points to css.php?friendly_id=screen-style

例如/css/screen-style.css指向css.php?friendly_id = screen-style

#4


You can use .php files in JavaScript and CSS calls. It's not pretty and anyone looking at your source knows it's a script, but it saves the hassle of configuration on the server. Also, if you're making dynamic JavaScript, I would suggest adding a timestamp on the end so the browser doesn't cache it.

您可以在JavaScript和CSS调用中使用.php文件。它并不漂亮,任何查看你的源代码的人都知道它是一个脚本,但它可以节省服务器上的配置麻烦。此外,如果您正在制作动态JavaScript,我建议在最后添加时间戳,以便浏览器不会对其进行缓存。

Example.

<script src="myjavascript.php?a=20090611-021213"></script>

#1


Put an .htaccess file in your /js/ folder and add the .js extension to PHP, like this:

将.htaccess文件放在/ js /文件夹中,并将.js扩展名添加到PHP,如下所示:

AddHandler application/x-httpd-php .js

In other words, have PHP parse all .js files as PHP files. So your scripts would really be PHP files on the server-side that output JavaScript. Do the same for stylesheets, only use the .css extension, obviously.

换句话说,让PHP将所有.js文件解析为PHP文件。所以你的脚本实际上是服务器端输出JavaScript的PHP文件。对样式表执行相同操作,显然只使用.css扩展名。

Note: I've never tried doing this in a separate .htaccess file. If it doesn't work, just put it into your global Apache config.

注意:我从未尝试在单独的.htaccess文件中执行此操作。如果它不起作用,只需将它放入您的全局Apache配置中。

#2


From my experience, rarely do you need to (and rarely should you) generate an entire script dynamically. For example, in javascript you may need to dynamically get some piece of data (like user info or settings) into javascript, but the rest of the script (classes/functions/DOM manipulations) is static across all users.

根据我的经验,您很少需要(并且很少应该)动态生成整个脚本。例如,在javascript中,您可能需要动态地将一些数据(如用户信息或设置)转换为javascript,但其余的脚本(类/函数/ DOM操作)在所有用户中都是静态的。

Typically in this case you would just want to put the dynamic stuff "inline", output dynamically from PHP and then include the js (the 95% that doesn't need dynamically generated) as an external script. The most obvious reason for this is caching the js/css.

通常在这种情况下,您只想将动态内容“内联”,从PHP动态输出,然后将js(不需要动态生成的95%)作为外部脚本包含在内。最明显的原因是缓存js / css。

Consider how reddit.com does it by looking at their source code for getting user data into javascript.

考虑一下reddit.com如何通过查看用于将用户数据获取到javascript的源代码来实现它。

var reddit = {
    /* is the user logged in */ logged: 'username',
    /* the subreddit's name (for posts) */ post_site: "",
    /* are we in an iframe */ cnameframe: false,
    /* this page's referer" */ referer: "",
    /* the user's voting hash */ modhash: 'lzbcszj9nl521385b7e075e9750ee4339547befc6a47fa01e6',
    /* current domain */ cur_domain: "reddit.com", ...
}

The rest of their js is found in external files.

其余的js可以在外部文件中找到。

#3


You could just use mod_rewrite to make certain php files be seen as CSS/JS

您可以使用mod_rewrite将某些php文件视为CSS / JS

e.g. /css/screen-style.css points to css.php?friendly_id=screen-style

例如/css/screen-style.css指向css.php?friendly_id = screen-style

#4


You can use .php files in JavaScript and CSS calls. It's not pretty and anyone looking at your source knows it's a script, but it saves the hassle of configuration on the server. Also, if you're making dynamic JavaScript, I would suggest adding a timestamp on the end so the browser doesn't cache it.

您可以在JavaScript和CSS调用中使用.php文件。它并不漂亮,任何查看你的源代码的人都知道它是一个脚本,但它可以节省服务器上的配置麻烦。此外,如果您正在制作动态JavaScript,我建议在最后添加时间戳,以便浏览器不会对其进行缓存。

Example.

<script src="myjavascript.php?a=20090611-021213"></script>