override or remove ready handler
Asked Answered
C

1

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

Cordwood answered 5/7, 2012 at 21:13 Comment(6)
Just out of curiosity, why can't you just edit the existing handler?Canikin
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.Tericaterina
@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.Cainozoic
@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 itCainozoic
Yo, flick me your code I might be able to help you out and please mention the behaviour you are expecting! :)Hereunder
@Hereunder - 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 mineCainozoic
C
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:-)

Cordwood answered 5/7, 2012 at 21:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.