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

6
  • Just out of curiosity, why can't you just edit the existing handler? Commented Jul 5, 2012 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. Commented Jul 5, 2012 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. Commented Jul 5, 2012 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 Commented Jul 5, 2012 at 21:35
  • Yo, flick me your code I might be able to help you out and please mention the behaviour you are expecting! :) Commented Jul 5, 2012 at 21:35

1 Answer 1

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:-)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.