最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Use javascript to check http status codes - Stack Overflow

programmeradmin3浏览0评论

I want to display profile pictures from gravatar for only those users who have a picture set. Doing this server side means doing around 100 HEAD requests to gravatar for checking 404 codes and appropriately outputting img tags for each request.

So, I want to implement a javascript function where I can just output 100 urls for which javascript can check the http status codes and output the appropriate image tags dynamically. Is that even possible? How?

I want to display profile pictures from gravatar for only those users who have a picture set. Doing this server side means doing around 100 HEAD requests to gravatar for checking 404 codes and appropriately outputting img tags for each request.

So, I want to implement a javascript function where I can just output 100 urls for which javascript can check the http status codes and output the appropriate image tags dynamically. Is that even possible? How?

Share Improve this question asked Dec 20, 2011 at 5:33 Deepak MittalDeepak Mittal 3381 gold badge4 silver badges10 bronze badges 2
  • However you happen to retrieve the image URL in Javascript will no doubt give you access to the request's status code. – Andrew Marshall Commented Dec 20, 2011 at 5:36
  • That's the question. How do I retrieve the image URLs status codes? Cross Domain Ajax? – Deepak Mittal Commented Dec 20, 2011 at 6:10
Add a ment  | 

1 Answer 1

Reset to default 7

The keyword you're missing is "status code" (that's what we collectively call all the HTTP response codes of 200, 404, 500, etc). I'm going to assume you're using jQuery, in which case, all the documentation you need for doing AJAX is at http://api.jquery./jQuery.ajax/

Here's a simple example for a request that displays an alert, but only if a 404 status code is returned (lifted almost verbatim the link above):

<script src="http://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
   $(function() {
      var url = "some_url";
      $.ajax(url,
      {
         statusCode: {
         404: function() {
            alert('page not found');
         }
      }
   });   
});
</script>
发布评论

评论列表(0)

  1. 暂无评论