Wednesday, April 17, 2013

jQuery / Javascript method runs several times after single call

Recently, I was developing web page and attached click handler to button, which was loading some content into div element. What was unexpected, after first click - it loaded everything just fine, but each next click was increasing amount of method calls.
As I found out, problem was that after successful method execution it also loaded default page template with every new fragment, including JavaScript page initialization scripts, which was adding new handlers to all old buttons, and so on, and so on.
It was easy to fix by creating new empty template specifically for HTML fragments.
I had this problem with Grails, but any template engine with too many conventions and defaults can be vulnerable.

No comments:

Post a Comment