JavaScript/jQuery 检查损坏的链接
我开发了一个小型 Javascript/jQuery 程序来访问 pdf 文件集合以供内部使用。我想突出显示 pdf 文件的信息 div(如果该文件确实存在)。
有没有一种方法可以以编程方式确定文件的链接是否已损坏?如果是这样,怎么办?
任何指南或建议均适用。
I developed a small Javascript/jQuery program to access a collection of pdf files for internal use. And I wanted to have the information div of a pdf file highlighted if the file actually exist.
Is there a way to programmatically determine if a link to a file is broken? If so, How?
Any guide or suggestion is appropriated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果文件位于同一域中,则可以使用 AJAX 来测试它们是否存在
以下是相关问题提供的简单方法:
If the files are on the same domain, then you can use AJAX to test for their existence as Alex Sexton said; however, you should not use the
GET
method, justHEAD
and then check the HTTP status for the expect value (200, or just less than 400).Here's a simple method provided from a related question:
问题是 JavaScript 具有同源策略,因此您无法从另一个域获取内容。这不会因为投票而改变(想知道 17 票)。
我认为你需要它作为外部链接,所以仅仅使用 .js 是不可能的......
Issue is that JavaScript has the same origin policy so you can not grab content from another domain. This won't change by upvoting it (wondering about the 17 votes).
I think you need it for external links, so it is impossible just with .js ...
如果文件不在外部网站上,您可以尝试对每个文件发出 ajax 请求。如果它返回失败,那么您就知道它不存在,否则,如果它完成和/或返回的时间超过给定阈值,您可以猜测它存在。它并不总是完美的,但通常“filenotfound”请求很快。
If the files are not on an external website, you could try making an ajax request for each file. If it comes back as a failure, then you know it doesn't exist, otherwise, if it completes and/or takes longer than a given threshold to return, you can guess that it exists. It's not always perfect, but generally 'filenotfound' requests are quick.
您可以使用
$.ajax
来获取它。如果文件不存在,您将收到 404 错误,然后您可以在错误回调中执行您需要的任何操作(UI 方面)。如何触发请求(计时器?)取决于您。当然,如果您也有能力执行一些服务器端编码,您可以执行单个 AJAX 请求 - 扫描目录,然后返回 JSON 格式的结果。You can
$.ajax
to it. If file does not exist you will get 404 error and then you can do whatever you need (UI-wise) in the error callback. It's up to you how to trigger the request (timer?) Of course if you also have ability to do some server-side coding you can do a single AJAX request - scan the directory and then return results as say JSON.正如塞巴斯蒂安所说,由于同源政策,这是不可能的。如果该网站可以(暂时)在公共域上发布,您可以使用其中的链接检查器服务之一。我支持 checkerr.org
Like Sebastian says it is not possible due to the same origin policy. If the site can be published (temporarily) on a public domain you could use one of the link checker services out there. I am behind checkerr.org
正如其他人提到的,由于 JavaScript 的同源策略,仅使用已接受答案中的函数是行不通的。解决此问题的方法是使用代理服务器。您不必为此使用自己的代理,您可以使用此服务,例如: https://cors -escape.herokuapp.com(代码此处)。
代码如下所示:
As others have mentioned, because of JavaScript's same origin policy, simply using the function from the accepted answer does not work. A workaround to this is to use a proxy server. You don't have to use your own proxy for this, you can use this service for example: https://cors-escape.herokuapp.com (code here).
The code looks like this: