When to use XMLHttpRequest and when to use IFrame

Feb 10, 2005 19:46 · 163 words · 1 minute read

Joel Webber dissects maps.google.com in Mapping Google. He spent a good deal of time getting into what all is being done by Google Maps. Here’s an interesting takeaway:

I mentioned before that there was some advantage to be had by using a hidden IFrame over making direct XMLHttp requests. One of these is that the IFrame’s state affects the back button. So every time you do a search, it creates a new history entry. This creates an excellent user experience, because pressing the back button always takes you back to the last major action you performed (and the forward button works just as well).

This leads to a good rule of thumb. If you’re updating the page with new or updated information, you most likely don’t want a history event. So, use XMLHttpRequest. On the other hand, if you’re working in resposne to a user request, it would often be better to use the IFrame approach in order to preserve back button behavior.