Неработающие ссылки проверки JavaScript/jQuery

Как это уже было сказано, это зависит от платформы.

В целом, я предпочитаю использовать только сам интерпретатор Python, который упаковывается для моей ОС, и установите все остальное в виртуальная среда , но это - совершенно другая история... Если Вы имеете установленный setuptools, установка большинства пакетов Python так же проста как:

easy_install pycurl

26
задан NawaMan 19 October 2009 в 22:06
поделиться

3 ответа

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);
});
57
ответ дан 28 November 2019 в 06:40
поделиться

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);
1
ответ дан 28 November 2019 в 06:40
поделиться

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.

0
ответ дан 28 November 2019 в 06:40
поделиться
Другие вопросы по тегам:

Похожие вопросы: