Mobile AJAX

jjore on 2007-02-08T20:55:30

I've just been informed that I'm to start porting an existing AJAX library over to run on mobile browsers. Oooooh arrrrgh. I envision much pain in my future.

Added later, it turns out I misunderstood what I was asked to do. It's nothing would have called "Mobile AJAX." Still, most of my test suite runs on my Samsung phone.


What pain?

bart on 2007-02-09T16:54:23

That sounds like fun.

BTW in case XMLHttpRequest doesn't work at all, I found an alternative that's just as good: creating a script element at runtime, which will get loaded as soon as you insert it into the HTML, as described here.

I found that the onload event works in Firefox but not in MSIE, so that approach is iffy... but a good alternative is have the script returned by the server call a function in your page, either at the end, or with the returned data as a parameter:

#!/usr/local/bin/perl
print "Content-type: text/javascript\n\n";
print <<'--END--';
callback(['one',
          'two',
          'three']);
--END--
where callback is a Javascript function you defined in your html.

This script works only once, you have to append a new script element every time you want to call it, but you can remove the previous one with removeElement.

And it appears document.body.appendChild(newScript) works as well as location and using the head element.