Skip to content

Instantly share code, notes, and snippets.

@silentguardian
Last active December 25, 2015 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silentguardian/7005884 to your computer and use it in GitHub Desktop.
Save silentguardian/7005884 to your computer and use it in GitHub Desktop.
<img class="userimg" data-src="http://i.imgur.com/a8oCZdF.jpg" src="http://i.imgur.com/ad2hK61.gif"><br />
<script>
$(document).ready(function() {
// http://api.jquery.com/jquery.ajaxtransport/
$.ajaxTransport("image", function(s) {
if (s.type === "GET" && s.async) {
var image;
return {
send: function(_ , callback) {
image = new Image();
function done(status) {
if (image) {
var statusText = (status === 200) ? "success" : "error", tmp = image;
image = image.onreadystatechange = image.onerror = image.onload = null;
callback(status, statusText, {image: tmp});
}
}
image.onreadystatechange = image.onload = function() {
done(200);
};
image.onerror = function() {
done(404);
};
image.src = s.url;
},
abort: function() {
if (image) {
image = image.onreadystatechange = image.onerror = image.onload = null;
}
}
};
}
});
$("img.userimg").each(function() {
var current_image = $(this);
if (current_image.data("src") != undefined) {
$.ajax({
username: "dummy",
password: "dummy",
url: current_image.data("src"),
type: "GET",
cache: true,
dataType: "image",
processData: "false",
}).done(function() {
current_image.attr("src", current_image.data("src"));
}).fail(function() {
current_image.attr("src", "http://i.imgur.com/mfxjbZM.png");
});
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment