6

I have to modify site that is using jQuery's ready handler, I wan't to reuse code and not have to write it again, however I need to change the behaviour of original ready handler, the question is:

  • how to remove ready handler (to apply new one)?
  • or how to override existing ready handler (original has anonymous function used)?

best regards

Lukasz 'Severiaan' Grela
  • 5,518
  • 4
  • 34
  • 69
  • Just out of curiosity, why can't you just edit the existing handler? – Mario S Jul 05 '12 at 21:16
  • There's no way to remove a ready handler short of physically removing the code for it. However, do you need to completely override the handler or do you just need to do additional stuff? You can call `$(document).ready()` multiple times, so there's nothing stopping you from treating it as if there were no other handlers active if you just need to do your own stuff. – Chris Pratt Jul 05 '12 at 21:21
  • @Mario - as I do only part of the site and I have to modify current functionality while keeping original files intact, basicaly to reuse data and functionality. – Lukasz 'Severiaan' Grela Jul 05 '12 at 21:34
  • @Chris Pratt - get rid of it as when it executes it throws errors, the case is that I'm building flash fallback for canvas, and I need to use existing functionality and jsut modify the places where the canvas was expected, tough luck, most of it is in the ready handler:) so get rid of it – Lukasz 'Severiaan' Grela Jul 05 '12 at 21:35
  • Yo, flick me your code I might be able to help you out and please mention the behaviour you are expecting! `:)` – Tats_innit Jul 05 '12 at 21:35
  • @Tats_innit - thanks, but I will go for the bind() unbind() usage, I will tell other dev to modify one line in his code:) so I will be able to work with mine – Lukasz 'Severiaan' Grela Jul 05 '12 at 21:38
  • 1
    Cool or ready this article: http://www.novogeek.com/post/Overriding-jQueryJavaScript-functions-using-closures.aspx you will be rocking! see you around! – Tats_innit Jul 05 '12 at 21:42

1 Answers1

5

Ok, apparently you can't unbind the ready handler, to be precise, you can't when the ready() is used but you can using the bind('ready', handler) so instead of

$(document).ready(handler);

use

$(document).bind('ready', handler);

then whenever you want to change the existing event handler use:

$(document).unbind('ready', handler);//when there is reference to the handler

or

$(document).unbind('ready');//to remove all handlers for ready event

as I've recently found you can add the event namespace:) to only remove ready events in that namespace (i've peeked the jplayer code) as follows:

var GD_EVENT_NAMESPACE = ".GrelaDesign";
$document.bind('ready' + GD_EVENT_NAMESPACE, function(){ alert('ready'); });
//later
$document.unbind('ready' + GD_EVENT_NAMESPACE);//this will remove only specific events

best regards

p.s. I was searching extensively before asking here:) and soon after I've asked I've found on bing the answer:-)

Lukasz 'Severiaan' Grela
  • 5,518
  • 4
  • 34
  • 69