Knowledge Base

Adding AJAX

What is Ajax?

Ajax (Asynchronous JavaScript And XML) is a technology that allows a web page to perform actions or update dynamically, without completely reloading. The idea is that by using Ajax technology, a website can be more responsive and interactive than a non-Ajax site. For instance, without Ajax, Google Maps might require you to click a link on the left side of the map and wait for the page to reload in order to scroll the map to the left. With Ajax, you can instead simply drag the map to scroll, and while there might be a small delay, it is a much shorter delay and less disruptive process than if you had to wait for the entire page to load.

If you already know about Ajax, and want to use it in a plugin you are developing, skip to AJAX in Plugins.

How does Ajax work?

Ajax technology is actually fairly simple to describe. Basically, the web programmer connects a link, button, or some other type of user interface element on the web page to a JavaScript program. When the user activates the interface (clicks on the link, drags the mouse, etc.), the JavaScript program sends some information or a request for data to a URL on the web server. Next, a program on the web server (probably also created by the web programmer) processes the request and sends back a response or some data. Typically the data is in XML format, but that is not actually required. When the data or response arrives in the browser, the JavaScript program receives an „asynchronous” notification, so that it can be processed or displayed appropriately.

Today, Ajax is supported in some form by most visual web browsers (though only if the user has JavaScript turned on). Of course, like most complex JavaScript, there are differences between how browsers implement JavaScript. However, these are not too difficult to overcome. The only real restriction on Ajax is that the URL you send the information to has to be on the same website as the URL the JavaScript came from. Other than that, the uses for the technology are only limited to what you can figure out how to program.

Ajax in WordPress

Because of its responsiveness, Ajax technology is being adopted by all sorts of websites — and WordPress is no exception. Currently, the core of WordPress uses Ajax only in the administration screens. For instance, Ajax is used for instant updates when you are doing comment moderation, and when you are adding and deleting items from lists such as categories, blogroll, and posts; Ajax is also the technology behind the auto-save functionality on post and page editing screens. Several themes and plugins also use Ajax; for instance, some post rating plugins use Ajax to store the visitor’s rating in the database and then display an updated average rating.