javascript - detect external broken links - Stack Overflow

The problem is that I have on my website many of exetrnal download links, and some of the links get exp

The problem is that I have on my website many of exetrnal download links, and some of the links get expired, so I want to detect automatically the expired links.

for me a valid link is a direct file download link pointing to one of my file servers. a broken link lead to a simple html page with an error message.

my first idea was to get the html source code of the download link and see if it contains an error but it did not work. I've tried also javascript but the problem is that js do not deal with external links.

any ideas?? thanks a lot

The problem is that I have on my website many of exetrnal download links, and some of the links get expired, so I want to detect automatically the expired links.

for me a valid link is a direct file download link pointing to one of my file servers. a broken link lead to a simple html page with an error message.

my first idea was to get the html source code of the download link and see if it contains an error but it did not work. I've tried also javascript but the problem is that js do not deal with external links.

any ideas?? thanks a lot

Share Improve this question asked Sep 30, 2012 at 15:55 user1709976user1709976 571 silver badge5 bronze badges 2
  • You could always send an AJAX request for every URL you're looking at, and see if you get a 404, 500, etc. error. – Ian Commented Sep 30, 2012 at 16:01
  • 2 doing this clientside for every link and for every client which visits your site is a big overhead, why not check it once a day on the serverside if a link is broken and remove it from the database? – supernova Commented Sep 30, 2012 at 16:02
Add a ment  | 

3 Answers 3

Reset to default 2

if you dont mind letting the client do the work, you could try doing it with javascript.

i have a greasemonkey script that automatically checks all links in the open page, and mark them according to the server response (not found, forbidden, etc).

see if you can get some ideas from it: http://userscripts/scripts/show/77701

i know that cross domain policies do not apply to GM_xmlhttprequest, and if want to use a javascript solution, might have to try a workaround, like:

  • Is it possible to use XMLHttpRequest across Domains
  • Making JavaScript call across domains
  • cross domain XMLHttprequest

if you want a server side solution, i believe the above answer can help you.

This isn't a task for your front-end, but for the back-end. As supernova said, check it from your server once a day. AJAX requests will not be your answer, since the browser security policy doesn't allow requests to different domains.


Solution:

Ok, based on your ment, check this solution:

<html>
<head>
  <script src='http://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js'></script>
  <script>
    $(document).ready(function(){
      var linksDiv = $('#links');

      $('#generateLinks').click(function(){
        //I don't know your logic for this function, so I'll try to reproduce the same behavior
        var someURLs = ['http://www.google.','http://www.djfhdkjshjkfjhk.', 'http://www.yahoo.'];
        linksDiv.html('');

        for(var i = 0; i < someURLs.length; i++){
          var link = $('<a/>').attr('href', someURLs[i]).append('link ' + i).css('display','block');
          linksDiv.append(link);
        }

      });

      $('#getLinksAndSend').click(function(){
        var links = linksDiv.find('a');

        var gatheredLinks = [];

        $(links).each(function(){
          gatheredLinks.push(this.href);
        });

        sendLinks(gatheredLinks);
      });

      var sendLinks = function(links){
        $.ajax({
          url: "your_url",
          type: "POST",
          data: {
            links: links
          }
        }).done(function(resp) { 
          alert('Ok!')
        });
      }
    });
  </script>
</head>
<body>
  <div id="links">

  </div> 
  <button id="generateLinks">Generate all links</button>
  <button id="getLinksAndSend">Get links and send to validator</button>
</body>
</html>

It may be overkill but there's a program in linux kde called klinkstatus that can find broken links in a website:

https://www.kde/applications/development/klinkstatus/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744582683a4582131.html

相关推荐

  • javascript - detect external broken links - Stack Overflow

    The problem is that I have on my website many of exetrnal download links, and some of the links get exp

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信