Monday, September 17, 2007

Ajax onreadystatechange Arguments

Having not used Ajax that much I was surprised to see that the function you declare for the onreadystatechange event could not take any arguments. I thought that this was a bit of a limitation as it meant that if you wanted that function to use any external data you had to declare that data globally, which has always been a big no-no in my book.

Global data when it's not needed always leads to a lot of pain in my experience.

But despair not, there is a way to pass arguments in. As Javascript supports anonymous methods you can create a new method inline, and then call another function from there--one that does have the arguments.

Take a look at this article if you're still stuck.

No comments: