Usage example
$('#myImg').safeUrl({wanted:"http://example/nature.png",rm:"/myproject/images/anonym.png"});
API:
$.fn.safeUrl=function(args){ var that=this; if($(that).attr('data-safeurl') && $(that).attr('data-safeurl') === 'found'){ return that; }else{ $.ajax({ url:args.wanted, type:'HEAD', error: function(){ $(that).attr('src',args.rm) }, success: function(){ $(that).attr('src',args.wanted) $(that).attr('data-safeurl','found'); } }); } return that; };
Note: rm means risk management here.
Another use case:
$('#myImg').safeUrl({wanted:"http://example/1.png",rm:"http://example/2.png"}) .safeUrl({wanted:"http://example/2.png",rm:"http://example/3.png"});
Abdennour TOUMI Nov 09 '14 at 13:22 2014-11-09 13:22
source share