开发者

Error handling image src from RSS feed

I'm using this PaRSS jQuery plugin to pull images from a bundl开发者_运维技巧e of RSS feeds. Now, some of the image matches returned are just a word (but image src can sometimes be found elsewhere in the feed).

How would I go about writing an error handler for this that:

  • checks against JPG, PNG, GIF
  • if the above is false, finds the correct src path somewhere else in the RSS feed
  • if an image is still not found, show a dummy image

This is the function that runs a match for images:

function getImageFromContent(content) {
      var img = content.match(/<img[^>+]*>/i);
      if(img) {
        var source = img[0].match(/src="[^"+]*"/i),
        alt = img[0].match(/alt="[^"+]*"/i);
        return "<img " + source + " " + alt + " />";
      }
      return false;
    }

A few hints as to how I might go about this, would be greatly appreciated.


if you want to check whether an image's src url ends in jpg, png or gif, you can try matching the <img> tag returned by getImageFromContent()

var img = getImageFromContent(content);
var src = img.match(/src="([^"?]*)("|\?)/i)
var isProperUrl = !!src[1].match(/\.(jpg|png|gif)$/i);

isProperUrl will be true if the src ends with jpg, png, or gif.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜