Once again I want to load a page which contains its own script into a div using $(\"divid\").load(...). The problem I face is related to events. Let\'s say we trigger(\"mo
About a hundred years too late, but if you're not using anonymous functions you can do this much more simply by using unbind first:
$.fn.eventWillOnlySubscribeOnce = function () {
return this.each(function () {
var oneHandler = (function () {
HANDLER CODE
});
$(this).unbind("submit", oneHandler);
$(this).bind("submit", oneHandler);
});
};
This implementation will work in Firefox 4, but not in many other browsers - because the handler variable is created new each time so the unbind can't find a matching handler to unbind. Moving the handler into the parent scope fixes the problem: i.e.
var oneHandler = (function () {
HANDLER CODE
});
$.fn.eventWillOnlySubscribeOnce = function () {
return this.each(function () {
$(this).unbind("submit", oneHandler);
$(this).bind("submit", oneHandler);
});
};
Presumably Firefox is doing some clever optimisation which means the handler is held as a constant variable somewhere as it never changes.