jQuery Playground: The base of AJAX

A very simple trick…

HTML:
<img id="some-image" src="http://domain.com/image.jpg" alt="" width="200" height="70" />

Script:
var someimage = jQuery("img[id='some-image']");
jQuery.get( someimage.attr("src"), function( ) {
// Do something
});

This (way of client requests) is the base of  AJAX/XMLHttpRequest, in this case is just an image but it can be any kind resource (code, json, java, xml, html) and can be processed asynchronous by the browser on any way to do whatever you want (send data, get data, play with the DOM, etc)…

Note: as this code is written it could cause the resource to load 2 times, so if it is not intended just use another attribute into the img tag, or other variable to get the URL…
Also for simpler things use javascript directly… or jQuery only if you already loaded it for other things… but it senseless load a 100k library to run 2 lines of code, hehe…

AC.

This entry was posted in Basic Technical and tagged , , , . Bookmark the permalink.

Comments are closed.