Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

override or remove ready handler

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

like image 425
Lukasz 'Severiaan' Grela Avatar asked Jul 05 '12 21:07

Lukasz 'Severiaan' Grela


1 Answers

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

like image 117
Lukasz 'Severiaan' Grela Avatar answered Oct 09 '22 07:10

Lukasz 'Severiaan' Grela