JavaScript/jQuery 检查损坏的链接

发布于 2024-08-08 11:00:34 字数 153 浏览 3 评论 0原文

我开发了一个小型 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 技术交流群。

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

发布评论

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

评论(6

谁的年少不轻狂 2024-08-15 11:00:34

如果文件位于同一域中,则可以使用 AJAX 来测试它们是否存在

以下是相关问题提供的简单方法:

function urlExists(url, callback) {
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
      callback(xhr.status < 400);
    }
  };
  xhr.open('HEAD', url);
  xhr.send();
}

urlExists(someUrl, function(exists) {
    console.log('"%s" exists?', someUrl, exists);
});

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, just HEAD 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:

function urlExists(url, callback) {
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
      callback(xhr.status < 400);
    }
  };
  xhr.open('HEAD', url);
  xhr.send();
}

urlExists(someUrl, function(exists) {
    console.log('"%s" exists?', someUrl, exists);
});
寻找我们的幸福 2024-08-15 11:00:34

问题是 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 ...

提赋 2024-08-15 11:00:34

如果文件不在外部网站上,您可以尝试对每个文件发出 ajax 请求。如果它返回失败,那么您就知道它不存在,否则,如果它完成和/或返回的时间超过给定阈值,您可以猜测它存在。它并不总是完美的,但通常“filenotfound”请求很快。

var threshold   = 500,
    successFunc = function(){ console.log('It exists!'); };

var myXHR = $.ajax({
  url: $('#checkme').attr('href'),
  type: 'text',
  method: 'get',
  error: function() {
    console.log('file does not exist');
  },
  success: successFunc
});

setTimeout(function(){
  myXHR.abort();
  successFunc();
}, threshold);

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.

var threshold   = 500,
    successFunc = function(){ console.log('It exists!'); };

var myXHR = $.ajax({
  url: $('#checkme').attr('href'),
  type: 'text',
  method: 'get',
  error: function() {
    console.log('file does not exist');
  },
  success: successFunc
});

setTimeout(function(){
  myXHR.abort();
  successFunc();
}, threshold);
浅唱々樱花落 2024-08-15 11:00:34

您可以使用 $.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.

愁以何悠 2024-08-15 11:00:34

正如塞巴斯蒂安所说,由于同源政策,这是不可能的。如果该网站可以(暂时)在公共域上发布,您可以使用其中的链接检查器服务之一。我支持 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

不必在意 2024-08-15 11:00:34

正如其他人提到的,由于 JavaScript 的同源策略,仅使用已接受答案中的函数是行不通的。解决此问题的方法是使用代理服务器。您不必为此使用自己的代理,您可以使用此服务,例如: https://cors -escape.herokuapp.com(代码此处)。

代码如下所示:

var proxyUrl = "https://cors-anywhere.herokuapp.com/";

function urlExists(url, callback) {
  var sameOriginURL = proxyUrl + url;
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
      callback(xhr.status < 400);
    }
  };
  xhr.open('HEAD', sameOriginURL);
  xhr.send();
}

urlExists(someUrl, function(exists) {
  console.log('"%s" exists?', someUrl, exists);
});

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:

var proxyUrl = "https://cors-anywhere.herokuapp.com/";

function urlExists(url, callback) {
  var sameOriginURL = proxyUrl + url;
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
      callback(xhr.status < 400);
    }
  };
  xhr.open('HEAD', sameOriginURL);
  xhr.send();
}

urlExists(someUrl, function(exists) {
  console.log('"%s" exists?', someUrl, exists);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文