捕获所有 javascript

假设我有一个像这样的目录结构

/var/www
└── test.js

也许在javascript中加载脚本的最常见方法是通过

<script src='test.js'></script>

现在假设我不断地移动文件。现在目录结构看起来像

/var/www
└── foobar
    └── test.js

而不是遍历每个文件并将路径更改为

<script src='foobar/test.js'></script>

我想“捕获”服务器(apache2)上的

现在,在人们开始质疑我的前提之前,让我声明两件事:

-我无法控制文件何时移动,以及在哪里

-我可以设置一个 php 脚本来查找文件并返回客户端的完整路径,但是,这将需要一个额外的 xmlhttprequest 调用。


编辑 我需要捕获所有请求的原因是为了减少请求总数。目前,我正在查询服务器以查找脚本的完整路径名(第一个请求),然后将该路径名传递给

Suppose I have a directory structure like so

/var/www
└── test.js

Probably the most common way to load scripts in javascript is via

<script src='test.js'></script>

Now suppose I am constantly moving files around. Now the directory structure looks like

/var/www
└── foobar
    └── test.js

Rather than going through every file and changing the paths to

<script src='foobar/test.js'></script>

I want to 'catch' the <script> request on the server (apache2), and look for the test.js file. This search would be very much like how the shell uses the PATH variable to search for commands. Is there anyway to do this?

Now before people start questioning my premise, let me state a two things:

-I can't control when the files are moved around, and where

-I can set up a php script to find the file and return the full path to the client, however, this would require one additional xmlhttprequest call.


EDIT The reason I need to capture all requests is to reduce the total number of requests. Currently, I am querying the server to find the full path name of the script (first request), and then I am passing that path name to <script src=...> (second request). Instead, what I want is to call <script src='test.js'>, then have the server look for 'test.js' in the current folder, and if that doesn't work, look in all other specified folders, in this case foobar/. Once it finds the script, it would return it, thus requiring only one request.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久夏青 2024-12-26 15:34:53

您可以设置一个 .htaccess 文件,将所有 js 请求重定向到一个文件。这样,您的所有脚本都将被重定向到服务器端页面,该页面根据脚本名称决定要传递的 js 脚本:

htaccess :

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)\.js$ server_side_script.php [NC,L]

you can setup a .htaccess file that redirects all your js requests to one file. This way, all your scripts will be redirected to your serverside page that decides what js script to deliver based on the script's name :

htaccess :

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)\.js$ server_side_script.php [NC,L]
萧瑟寒风 2024-12-26 15:34:53

我知道执行此操作的唯一方法是设置自定义 404 处理程序。创建文件的最简单方法是使用 .htaccess 文件。添加如下内容:

ErrorDocument 404 /url.php

然后在 url.php 中,您将“找到”.js 文件并输出内容。确保将 HTTP 标头设置为 200:

<?php
//
header("HTTP/1.0 200 OK");
...
...
?>

上面假设当页面加载时,最初不会找到 .js 文件(这将启动 404 处理程序)。

The only way I would know to do this is to setup a custom 404 handler. The easiest way of creating one would be using an .htaccess file. Add something like:

ErrorDocument 404 /url.php

Then within url.php you would 'find' the .js file and output the contents. Make sure to set the HTTP header to 200:

<?php
//
header("HTTP/1.0 200 OK");
...
...
?>

The above assumes that when the page loads the .js file won't be initially found (which will launch the 404 handler).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文