What to Consider When Using AJAX
A few tips to help you while you are learning.
•Consider the overhead
Your AJAX return should be concise and to the point. If it returns too much, your web page will slow down noticeably and that will defeat the purpose of using AJAX in the first place. Remember, people still have to use dial-up modems.
•Test your output and test it often
If you do not test the AJAX page before your page that will call it, you will have trouble and confusion later on. You need a way to enter and test data on the AJAX page so that it returns the results you want. Never assume it does.
•Start small, and then expand
If your AJAX page is going to be returning a lot of information, don't try to make it do all that the first go around. Get part of it working and working well, then go on to the next thing and get that working well. But do not expand too much. AJAX is supposed to be efficient, otherwise it can actually be slower than a traditional approach.
•Remember the variable's scope
Variables defined in a function, stay in a function. When the function is finished, so is the variable. Variables defined outside any function are global, available to everybody everywhere. As a programming best practice, it is best to avoid global variables. Global variables have their uses, but keep them at a minimum. Variables defined in an object are only available through the object.
•Remember no one method is ideal for every situation
AJAX requests may be executed twice, so for anything that can only be executed once, use a hidden iframe instead of AJAX. Asynchronous requests allow the web page to continue doing other tasks, but do not work when making several requests quickly. If your JavaScript has a loop of some kind to make several AJAX requests, use the synchronous method instead. Get requests are easier but have limitations on the amount of data that can be sent.
•You cannot perform incremental actions asynchronously
There are situations where you must use smaller requests. If you use an asynchronous request, all your requests will be performed almost simulatenously. This will overwhelm your web server and your database if you are making database requests. The result will be most of your requests will not be executed.
•Do not be afraid to ask for help
The internet is full of volunteers helping people like you. Ask and use their help. Quite often, such help is as good or better than what you find in a book. However, before asking for help, search to see if someone already asked your same question. That will save time waiting for a response.
Techs On Call * 252-452-1874 * 919-521-5122 * 866-604-9222 * service@techs-on-call.biz
PO Box 1166 * Nashville, NC 27856-2166
Service Policies
