Reader's digest version: What is the most efficient way of requesting an image from a server and placing it into the DOM using AJAX (or AJAI, perhaps? : ) )? Here's the long version:
Hello SO, I'm setting up a simple page that has a list of items, and when you click on one, it does the following:
- Clear the modal dialog on the page (which is hidden) of the image previously in it (if any)
- Request the page of /url/ajax/some-image/
- Open the modal dialog with a spinner showing
- Once the request to the server finishes: inject the HTML from that page into the modal dialog
The page that's requested above searches through a database to find the entry that matches the URL given, and returns an HTML page that says <img src="path/to/image.jpg" alt="Whatever this would be." />. I'm not sure if jQuery is saving me with some magic to make this more efficient, but it seems to me that requesting a page that requests another image is doing more work than needs to be done. What I'm looking for would be something more like this:
- Clear the modal dialog of the image previously in it
- Request the image at /url/ajax/some-image/(I can set this to return the image itself, instead of an<img/>tag for the image)
- Open the modal dialog with a spinner showing
- Once the request to the server finishes: inject the image directly into the modal dialog
The problem I'm having is... how? I was looking at $.get, which seems to make the most sense to me for this and I'm just in a bit too far over my head--I'm quite new to JavaScript/jQuery.
I've set the value for the some-image part of the URL to the rel value of the <a> that it corresponds to (to clarify: this is done server-side), so I can build the URL to the image pretty easily within the click() event's function when it's fired, the modal dialog is just div#dialog. Could this be done with something as simple as changing the src of the <img/>?