Hello.
How to use JavaScript
determine if the link to the image is working or not?
|
3 answers
You write a test_link.php file in which you check with curl whether the link works. Further, in js you make a request for test_link.php via ajax. You get the result, you rejoice :) With the help of pure js you can’t do this.
- OK thanks. It’s not hard for you to bring this PHP code. I just do not work with him, and I don’t know any principles of his work. Those. Just so that from beginning to end, it would be possible to save and earn. - Anton Mukhin
- onew-shadow.com/blog/2007/08/02/… Here is an example of such a function. Just create a file check_page.php, inside add: <? Php function page_exists ($ url) {// here is the function code from the example} if (page_exists ($ _ POST ['url'])) echo "1"; else echo "0"; ?> Something like this and further, when you call this page with the POST parameter, url = page_address if the page address exists, 1 will be returned, if not, 0. Then, from the js side, you check that there is ajax call back. - Drakmail
|
For example, on jQuery :
$(function(){ //On DOM ready $('#img').error(function(){ alert('error'); }); });
HTML:
<img src="fafav.jpg" id="img" />
- Will work only within the domain. Ps. Although no, it works with cross-domain overgrowths. But here a minus - only pictures can be checked. - Drakmail
- > How to use JavaScript to determine if the link is working or not? In the context of the task there is no minus. The advantages are obvious: you do not need to write an extra server-side code, process ajax requests. - caravaneer
- Hm I just need pictures. And what will it look like without using libraries? - Anton Mukhin
- onejsfiddle.net/NHvF6 - caravaneer
- It worked! - Anton Mukhin
|
For links within the same domain:
function TestURL(url) { var request = new XMLHttpRequest(); request.open('HEAD', url, false); request.send(); return request.status != 404; }
|