Image() class. When we create a new
Image() object, the browser creates an internal image just like any other image on the page. If we set an “src” property of the object, with a URL pointing to the location of the image file, the browser will start downloading the resource — content type be damned.
Here’s a quick demo script:
var myResource = new Image(); myResource.src = "somefile.html";
The moment this is executed,
somefile.html will be downloaded from the server. Try it out, and watch the HTTP request fly past (Firebug’s Net panel or Firefox’s LiveHttpHeaders extension are handy here). The URI doesn’t have to be relative, either. It works perfectly cross-domain:
There are a few drawbacks to this approach. The first is, of course, that we can’t work with the data fetched through the request — just as we can’t work with the binary data of an image (nor would we want to). Second, there is no real mechanism to monitor the request – not even HTTP status codes. Of course, this isn’t a problem if you have total control over the server at the other end.