捕获所有 javascript
假设我有一个像这样的目录结构
/var/www
└── test.js
也许在javascript中加载脚本的最常见方法是通过
<script src='test.js'></script>
现在假设我不断地移动文件。现在目录结构看起来像
/var/www
└── foobar
└── test.js
而不是遍历每个文件并将路径更改为
<script src='foobar/test.js'></script>
我想“捕获”服务器(apache2)上的 请求,并查找
test.js
文件。此搜索非常类似于 shell 如何使用 PATH 变量来搜索命令。有办法做到这一点吗?
现在,在人们开始质疑我的前提之前,让我声明两件事:
-我无法控制文件何时移动,以及在哪里
-我可以设置一个 php 脚本来查找文件并返回客户端的完整路径,但是,这将需要一个额外的 xmlhttprequest 调用。
编辑 我需要捕获所有请求的原因是为了减少请求总数。目前,我正在查询服务器以查找脚本的完整路径名(第一个请求),然后将该路径名传递给 (第二个请求)。相反,我想要的是调用
,然后让服务器在当前文件夹中查找“test.js”,如果没有工作,查看所有其他指定的文件夹,在本例中为
foobar/
。一旦找到脚本,它就会返回它,因此只需要一个请求。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以设置一个 .htaccess 文件,将所有
js
请求重定向到一个文件。这样,您的所有脚本都将被重定向到服务器端页面,该页面根据脚本名称决定要传递的 js 脚本:htaccess :
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 :
我知道执行此操作的唯一方法是设置自定义 404 处理程序。创建文件的最简单方法是使用 .htaccess 文件。添加如下内容:
然后在 url.php 中,您将“找到”.js 文件并输出内容。确保将 HTTP 标头设置为 200:
上面假设当页面加载时,最初不会找到 .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:
Then within url.php you would 'find' the .js file and output the contents. Make sure to set the HTTP header to 200:
The above assumes that when the page loads the .js file won't be initially found (which will launch the 404 handler).