Reputation: 57
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
Upvotes: 2
Views: 7816
Reputation: 11
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.org/applications/development/klinkstatus/
Upvotes: 0
Reputation: 9576
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 comment, check this solution:
<html>
<head>
<script src='http://ajax.googleapis.com/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.com','http://www.djfhdkjshjkfjhk.com', 'http://www.yahoo.com'];
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>
Upvotes: 2
Reputation: 6232
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.org/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:
if you want a server side solution, i believe the above answer can help you.
Upvotes: 2