Last active
August 29, 2015 14:08
-
-
Save LouWii/e940c94c9e126411819e to your computer and use it in GitHub Desktop.
Verify links to files, to know if accessible or not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- This simple piece of code will look for specific links, verify them using HEAD request to know if it exists/is accessible or not --> | |
<!-- Every failed files will be displayed into the failed-list <span> --> | |
<!-- NOTE : This script might "freeze" your browser for a while, don't worry, let it finish --> | |
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script> | |
<button id="launch-link-test">Test all links</button> | |
<li><a target="_blank" data-count="0" href="link-to-file.pdf">Link 0</a><span id="url-valid-0"></span></li> | |
<li><a target="_blank" data-count="1" href="link-to-file1.pdf">Link 1</a><span id="url-valid-1"></span></li> | |
<li><a target="_blank" data-count="2" href="link-to-file2.pdf">Link 2</a><span id="url-valid-2"></span></li> | |
<li><a target="_blank" data-count="3" href="link-to-file3.pdf">Link 3</a><span id="url-valid-3"></span></li> | |
<span id="failed-list"> | |
</span> | |
<script type="text/javascript"> | |
$(function() { | |
$('#launch-link-test').click(function(){ | |
var $links = $('a[data-count]'); | |
for (i = 0; i < $links.length; i++) | |
{ | |
if (UrlExists($links[i].href)) | |
{ | |
$('#url-valid-'+$($links[i]).data('count'))[0].innerHTML = "OK"; | |
} | |
else | |
{ | |
$('#url-valid-'+$($links[i]).data('count')).css('color', 'red').css('font-weight', 'bold'); | |
$('#url-valid-'+$($links[i]).data('count'))[0].innerHTML = "NOK"; | |
$('#failed-list').append('<p>'+$links[i].href+'</p>'); | |
} | |
} | |
}); | |
}); | |
function UrlExists(my_url) { | |
var http = new XMLHttpRequest(); | |
http.open('HEAD', my_url, false); | |
try | |
{ | |
http.send(); | |
} | |
catch(err) | |
{ | |
return false; | |
} | |
return http.status != 404; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment