Reputation: 53
I am using python urllib to get an image from an URL.
img=Image.open(urllib2.urlopen(URL))
What is the alternative method to do this in plain Javascript?
Upvotes: 1
Views: 384
Reputation: 23134
You can use an ajax call:
Edit because of comment:
var img = new Image();
$('#your-image-container').click(function() {
$.ajax({
url: URL,
type: "GET",
async: True,
success: function(in_img) {
img.src = in_img;
$(this).append(img);
},
})
}
That is a direct alternative.
You should also consider that in javascript, you can simply do:
$(function(){
$("#your-image-container").attr("src", URL)
});
and you will have the image loaded in the #your-image-container!
Upvotes: 1